Skip to Content
Hooks

usePreference

Subscribe to and update a persisted preference created with createPreference or createColorScheme.

Import

import { usePreference } from "@/components/ui/hacline/components/preferences";
import { createPreference } from "@/components/ui/hacline/components/preferences";

Usage

Pass a preference store created via createPreference (or createColorScheme) to usePreference to read the current value reactively and update it across tabs and reloads.

import { usePreference } from "@/components/ui/hacline/components/preferences";
import { createPreference } from "@/components/ui/hacline/components/preferences";

const density = createPreference({
  name: "density",
  values: ["comfortable", "compact"] as const,
  defaultValue: "comfortable",
});

export const DensityToggle = () => {
  const [value, setValue] = usePreference(density);

  return (
    <button
      onClick={() =>
        setValue(value === "comfortable" ? "compact" : "comfortable")
      }
      type="button"
    >
      Density: {value}
    </button>
  );
};