Layout & Disclosure
@hacline/ui/preferencesPreferences
Apply user preference tokens onto element data attributes.
Usage
Buttons update the layout preference created with createPreference. Preferences writes data-layout, which example styles use to adjust list columns.
- Requirements
- Deliverables
"use client";
import { useMemo } from "react";
import { Button } from "@/components/ui/hacline/components/button";
import {
createPreference,
Preferences,
usePreference,
} from "@/components/ui/hacline/components/preferences";
export const Example = () => {
const layout = useMemo(
() =>
createPreference({
attribute: "data-layout",
defaultValue: "list",
values: ["list", "grid"],
}),
[]
);
const { value } = usePreference(layout);
return (
<Preferences className="group" preferences={[layout]}>
<div className="grid w-full max-w-xs justify-items-start gap-2">
<Button
onClick={() => layout.set(value === "list" ? "grid" : "list")}
variant="outline"
>
{value === "list" ? "Switch to grid" : "Switch to list"}
</Button>
<ul className="grid w-full gap-2 group-data-[layout=grid]:grid-cols-2">
<li className="border-hl-border bg-hl-surface rounded-hl-md border p-2 text-sm">
Requirements
</li>
<li className="border-hl-border bg-hl-surface rounded-hl-md border p-2 text-sm">
Deliverables
</li>
</ul>
</div>
</Preferences>
);
};Scoped Theme & System Sync
Manage system, light, and dark preferences via createColorScheme so data-color-scheme styles only this region.
This theme applies only to this preview.
"use client";
import { useMemo } from "react";
import {
createColorScheme,
Preferences,
usePreference,
} from "@/components/ui/hacline/components/preferences";
import {
SegmentedControl,
SegmentedControlIndicator,
SegmentedControlItem,
} from "@/components/ui/hacline/components/segmented-control";
export const Example = () => {
const theme = useMemo(
() => createColorScheme({ defaultValue: "system" }),
[]
);
const { value } = usePreference(theme);
return (
<Preferences className="group" preferences={[theme]}>
<div className="grid w-full max-w-xs gap-2">
<SegmentedControl
aria-label="Scoped preview theme"
onValueChange={(next) => {
if (next) {
theme.set(next);
}
}}
value={value}
>
<SegmentedControlIndicator />
<SegmentedControlItem value="system">System</SegmentedControlItem>
<SegmentedControlItem value="light">Light</SegmentedControlItem>
<SegmentedControlItem value="dark">Dark</SegmentedControlItem>
</SegmentedControl>
<div className="rounded-hl-md border-hl-border bg-hl-surface text-hl-fg border p-3 text-sm">
This theme applies only to this preview.
</div>
</div>
</Preferences>
);
};Props
Prop
Type