Demo
Button attributes, classes and server rendering
Documented in Button.
Button attributes, classes and server rendering
className and style on a Button, DOM attributes passed through to the native button, a handler that vetoes the built-in toggle with preventDefault, and a status that shows the demo was rendered on the server and then hydrated.
Rendering:
Demo.tsx
'use client';
import { useState, useSyncExternalStore } from 'react';
import { Button, ToggleButton } from '@salmonui/buttons/button';
import { Checkbox } from '@salmonui/inputs/checkbox';
const noopSubscribe = () => () => {};
// className / style passthrough, DOM attributes, handler composition and an
// SSR status: the server snapshot says "server HTML", the client "hydrated".
export default function ButtonAttributesDemo() {
const hydrated = useSyncExternalStore(
noopSubscribe,
() => true,
() => false,
);
const [locked, setLocked] = useState(false);
const [pinned, setPinned] = useState(false);
const [archived, setArchived] = useState(0);
return (
<div style={{ display: 'grid', gap: 12 }}>
<div style={{ display: 'flex', flexWrap: 'wrap', alignItems: 'center', gap: 8 }}>
<Button
className="demo-archive-button"
style={{ minInlineSize: 140, letterSpacing: '0.02em' }}
data-analytics-id="archive"
aria-describedby="demo-button-attributes-hint"
onClick={() => setArchived((n) => n + 1)}
>
Archive ({archived})
</Button>
<ToggleButton
pressed={pinned}
onPressedChange={setPinned}
onClick={(event) => {
if (locked) event.preventDefault();
}}
>
{pinned ? 'Pinned' : 'Pin'}
</ToggleButton>
<Checkbox checked={locked} onCheckedChange={(checked) => setLocked(checked)}>
Lock pin
</Checkbox>
</div>
<p id="demo-button-attributes-hint" style={{ margin: 0 }}>Archived items stay searchable for 30 days.</p>
<p style={{ margin: 0 }}>
Rendering: <output>{hydrated ? 'hydrated' : 'server HTML'}</output>
</p>
</div>
);
}
Archive carries an application class, an inline style, a data-*
attribute and aria-describedby; inspect it to see them on the <button>
after salmon-button. With Lock pin checked, the pin button's own
onClick calls event.preventDefault(), so the built-in toggle is skipped.
The status line reads "server HTML" in the static markup and switches to
"hydrated" once React attaches, without a hydration warning.