@md-expressive/react
v0.0.3
Published
React components for @md-expressive/web-components (Material 3 Expressive).
Maintainers
Readme
@md-expressive/react
React components for @md-expressive/web-components,
Material 3 Expressive components modelled on Jetpack Compose. Works with React 18 and 19.
npm install @md-expressive/reactimport { useState } from 'react';
import { Button, Icon, Switch, applyTheme } from '@md-expressive/react';
applyTheme('#6750A4'); // generates the colour scheme on <html>
export function Settings() {
const [wifi, setWifi] = useState(true);
return (
<>
<Switch checked={wifi} onMdChange={(e) => setWifi(e.detail.checked)} />
<Button variant="tonal" onClick={() => setWifi(false)}>
<Icon slot="icon">wifi_off</Icon>Turn off
</Button>
</>
);
}Each element has a component named after its tag, without the md- (<md-text-field> is
TextField). Each one registers its custom element the first time it renders, so your bundle
only contains the components you import. The theming, font and motion utilities and every type
are re-exported, so you only need this package.
Props and events
- Props are set as element properties, so objects, arrays and functions work
(
isDateSelectable={(date) => …}onDatePicker). Their names are the camel-case property names from the web components' docs (supportingText, notsupporting-text). - Component events are
on+ the event name:mdChangeisonMdChange,mdSelectisonMdSelect. The handler gets theCustomEvent, with its data ine.detail. - Native events:
onClickandonInputwork on every React version. PreferonMdChangetoonChange: React 18 doesn't deliverchangeevents from custom elements. In controlled mode (below), useonMdChange/onMdInput. - Slots are the
slotattribute on a child, as in HTML:<Icon slot="icon">. - Methods (
show(),close(), …) are on the element; use aref.
Controlled components
Pass a component's value prop and it's controlled, exactly like React's native inputs: it only
ever shows your state. Each change the user makes is proposed to your handler, with the new value
in e.detail; set the state to accept it, set something else to replace it, or do nothing to
refuse it.
const [name, setName] = useState('');
<TextField label="Code" value={name} onMdInput={(e) => setName(e.detail.value.toUpperCase())} />
const [volume, setVolume] = useState(20);
<Slider value={volume} onMdInput={(e) => setVolume(Math.min(50, e.detail.value))} />| Component | Value prop | Handler |
|---|---|---|
| Switch, Checkbox, Radio | checked | onMdChange |
| Button, IconButton (toggles), Chip (filter), ListItem, MenuItem | selected | onMdChange |
| Tabs | selectedIndex | onMdChange |
| NavigationBar, NavigationRail | value | onMdChange |
| DatePicker | value, or start / end | onMdChange |
| DockedDatePicker, DateField | value | onMdChange |
| Slider, ScrollField, TextField, TimePicker | value | onMdInput |
| RangeSlider | valueStart, valueEnd | onMdInput |
Controlled components still feel native: a dragged thumb stays under the pointer, typed text keeps its caret, and a refused switch springs back. Your update is applied during the event (as React does for its own inputs), so fast typing and key repeats never work from a stale value.
Leave the value prop out and the component keeps its own state (uncontrolled); handlers then just report changes.
Server rendering
The components are client components (the module starts with 'use client'), so they work in
Next.js and other server-rendering frameworks, but they draw on the client: the server's HTML
doesn't contain their shadow DOM yet.
