@gemologic/sheen 0.3.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,11 +3,19 @@ import type { JSX } from "solid-js";
3
3
  import { cn } from "../utils/cn.ts";
4
4
  import { createBooleanInputState } from "./boolean-input-state.ts";
5
5
 
6
- export interface CheckboxProps extends Omit<JSX.HTMLAttributes<HTMLDivElement>, "children" | "onChange" | "id" | "ref" | "onPointerDown"> {
6
+ /** Exactly one accessible name source for the native input. */
7
+ export type BooleanControlName =
8
+ | { label: string; "aria-label"?: never; "aria-labelledby"?: never }
9
+ | { label?: never; "aria-label": string; "aria-labelledby"?: never }
10
+ | { label?: never; "aria-label"?: never; "aria-labelledby": string };
11
+
12
+ // Directives apply only to native elements, never through a component's props.
13
+ export interface BooleanControlBaseProps extends Omit<JSX.HTMLAttributes<HTMLDivElement>, "children" | "onChange" | "id" | "ref" | "onPointerDown" | "aria-label" | "aria-labelledby" | "aria-describedby" | `use:${string}`> {
7
14
  id?: string;
8
15
  ref?: HTMLDivElement | ((element: HTMLDivElement) => void);
9
16
  onPointerDown?: NonNullable<JSX.HTMLAttributes<HTMLDivElement>["onPointerDown"]>;
10
- label: string;
17
+ /** Extra description IDs for the native input, merged with the description and error. */
18
+ "aria-describedby"?: string | undefined;
11
19
  description?: string;
12
20
  error?: string;
13
21
  checked?: boolean;
@@ -23,23 +31,24 @@ export interface CheckboxProps extends Omit<JSX.HTMLAttributes<HTMLDivElement>,
23
31
  inputRef?: (element: HTMLInputElement) => void;
24
32
  }
25
33
 
34
+ export type CheckboxProps = BooleanControlBaseProps & BooleanControlName;
35
+
26
36
  export function createNativeBooleanControl(props: CheckboxProps, kind: "checkbox" | "switch"): JSX.Element {
27
37
  const [local, state, others] = splitProps(props,
28
- ["label", "description", "error", "class", "onCheckedChange", "inputRef", "form"],
38
+ ["label", "aria-label", "aria-labelledby", "aria-describedby", "description", "error", "class", "onCheckedChange", "inputRef", "form"],
29
39
  ["checked", "defaultChecked", "indeterminate", "disabled", "readOnly", "required", "name", "value"]);
30
40
  const prefix = `sheen-${kind}`;
31
41
  const generatedId = createUniqueId();
32
42
  const inputId = `${generatedId}-input`;
33
- let input: HTMLInputElement | undefined;
34
- const describedBy = () => [local.description ? `${generatedId}-description` : "", local.error ? `${generatedId}-error` : ""].filter(Boolean).join(" ") || undefined;
43
+ const describedBy = () => [local["aria-describedby"], local.description ? `${generatedId}-description` : "", local.error ? `${generatedId}-error` : ""].filter(Boolean).join(" ") || undefined;
35
44
  const { checked, requestChange, setInput } = createBooleanInputState(state, next => local.onCheckedChange?.(next));
36
45
  return <div {...others} class={cn(prefix, local.class)} data-checked={checked() || undefined} data-indeterminate={state.indeterminate || undefined} data-disabled={state.disabled || undefined} data-readonly={state.readOnly || undefined} data-invalid={local.error ? "" : undefined}>
37
46
  <div class={`${prefix}-row`}>
38
- <input id={inputId} type="checkbox" role={kind === "switch" ? "switch" : undefined} name={state.name ?? ""} value={state.value} form={local.form} disabled={state.disabled} required={state.required} readOnly={state.readOnly} checked={checked()} aria-readonly={state.readOnly || undefined} aria-invalid={local.error ? "true" : undefined} aria-describedby={describedBy()} class={`${prefix}-input`} ref={element => { input = element; setInput(element); local.inputRef?.(element); }} onChange={event => requestChange(event.currentTarget)} />
39
- <span class={`${prefix}-control`} data-indeterminate={state.indeterminate || undefined} aria-hidden="true" onClick={() => input?.click()}>
47
+ <input id={inputId} type="checkbox" role={kind === "switch" ? "switch" : undefined} name={state.name ?? ""} value={state.value} form={local.form} disabled={state.disabled} required={state.required} readOnly={state.readOnly} checked={checked()} aria-label={local["aria-label"]} aria-labelledby={local["aria-labelledby"]} aria-readonly={state.readOnly || undefined} aria-invalid={local.error ? "true" : undefined} aria-describedby={describedBy()} class={`${prefix}-input`} ref={element => { setInput(element); local.inputRef?.(element); }} onChange={event => requestChange(event.currentTarget)} />
48
+ <span class={`${prefix}-control`} data-indeterminate={state.indeterminate || undefined} aria-hidden="true">
40
49
  {kind === "checkbox" ? <span class="sheen-checkbox-indicator"><span class="sheen-checkbox-mark" /></span> : <span class="sheen-switch-thumb" />}
41
50
  </span>
42
- <label for={inputId} class={`${prefix}-label`}>{local.label}<Show when={state.required}><span aria-hidden="true"> *</span></Show></label>
51
+ <Show when={local.label}>{label => <label for={inputId} class={`${prefix}-label`}>{label()}<Show when={state.required}><span aria-hidden="true"> *</span></Show></label>}</Show>
43
52
  </div>
44
53
  <Show when={local.description}><p id={`${generatedId}-description`} class={`${prefix}-description`}>{local.description}</p></Show>
45
54
  <Show when={local.error}><p id={`${generatedId}-error`} class={`${prefix}-error`}>{local.error}</p></Show>
@@ -12,6 +12,10 @@ export interface CommandPaletteCommand {
12
12
  readonly group: string;
13
13
  readonly keywords?: readonly string[];
14
14
  readonly disabled?: boolean;
15
+ /** Renders decorative leading content, hidden from assistive technology. */
16
+ readonly icon?: () => JSX.Element;
17
+ /** Renders trailing content such as a shortcut hint or badge, exposed as the option's description. */
18
+ readonly trailing?: () => JSX.Element;
15
19
  readonly run: () => void | Promise<void>;
16
20
  }
17
21
 
@@ -208,9 +212,14 @@ export function CommandPalette(props: CommandPaletteProps): JSX.Element {
208
212
  };
209
213
  const item = (command: CommandPaletteCommand) => {
210
214
  const binding = () => shortcuts.bindings().find(value => `shortcut:${value.id}` === command.id);
215
+ const id = createUniqueId();
216
+ const described = () => command.trailing !== undefined || binding() !== undefined;
211
217
  return <Command.Item value={`${command.id} ${command.label}`} keywords={[...(command.keywords ?? [])]}
212
- {...(command.disabled === undefined ? {} : { disabled: command.disabled })} onSelect={() => select(command)}>
213
- <span>{command.label}</span><Show when={binding()}>{value => <Kbd>{value().displayKeys}</Kbd>}</Show>
218
+ {...(command.disabled === undefined ? {} : { disabled: command.disabled })} onSelect={() => select(command)}
219
+ aria-labelledby={`${id}-label`} aria-describedby={described() ? `${id}-trailing` : undefined}>
220
+ <Show when={command.icon !== undefined}><span class="sheen-command-icon" aria-hidden="true">{command.icon?.()}</span></Show>
221
+ <span id={`${id}-label`} class="sheen-command-label">{command.label}</span>
222
+ <Show when={described()}><span id={`${id}-trailing`} class="sheen-command-trailing">{command.trailing?.()}<Show when={binding()}>{value => <Kbd>{value().displayKeys}</Kbd>}</Show></span></Show>
214
223
  </Command.Item>;
215
224
  };
216
225
 
@@ -13,13 +13,14 @@ export interface PopoverProps extends ParentProps {
13
13
  open?: boolean;
14
14
  defaultOpen?: boolean;
15
15
  onOpenChange?: (open: boolean) => void;
16
+ disabled?: boolean;
16
17
  placement?: "top" | "top-start" | "top-end" | "bottom" | "bottom-start" | "bottom-end" | "left" | "right";
17
18
  class?: string;
18
19
  }
19
20
 
20
- function PopoverTrigger(props: ParentProps): JSX.Element {
21
+ function PopoverTrigger(props: ParentProps<{ disabled: boolean }>): JSX.Element {
21
22
  const context = Primitive.usePopoverContext();
22
- return <Button ref={context.setTriggerRef} variant="outline" data-sheen-popover-trigger=""
23
+ return <Button ref={context.setTriggerRef} variant="outline" data-sheen-popover-trigger="" disabled={props.disabled}
23
24
  aria-haspopup="dialog" aria-expanded={context.isOpen()} aria-controls={context.isOpen() ? context.contentId() : undefined}
24
25
  onPointerDown={event => { if (event.pointerType === "mouse") event.preventDefault(); }}
25
26
  onClick={() => context.toggle()} {...context.dataset()}>{props.children}</Button>;
@@ -31,14 +32,14 @@ export function Popover(props: PopoverProps): JSX.Element {
31
32
  const [draft, setDraft] = createSignal(props.defaultOpen ?? false);
32
33
  const open = () => props.open === undefined ? draft() : props.open;
33
34
  const change = (next: boolean) => {
34
- if (next === open()) return;
35
+ if (next === open() || (next && props.disabled)) return;
35
36
  if (props.open === undefined) setDraft(next);
36
37
  props.onOpenChange?.(next);
37
38
  };
38
39
  createEffect(() => { if (open()) onCleanup(theme.layers.register(id)); });
39
40
  const layer = createMemo<number>(previous => open() ? theme.layers.zIndex(id) : previous ?? 100);
40
41
  return <Primitive.Root open={open()} onOpenChange={change} placement={props.placement ?? "bottom"} gutter={6}>
41
- <PopoverTrigger>{props.trigger}</PopoverTrigger>
42
+ <PopoverTrigger disabled={props.disabled ?? false}>{props.trigger}</PopoverTrigger>
42
43
  <Show when={theme.portal()}>{target => <Primitive.Portal mount={target()}>
43
44
  <Primitive.Content data-kb-top-layer="true" class={cn("sheen-popover", props.class)} aria-hidden={!open() || undefined} inert={!open()} style={{ "z-index": layer() }}
44
45
  onEscapeKeyDown={event => { if (!theme.layers.isTop(id)) event.preventDefault(); }}>
@@ -1,8 +1,8 @@
1
1
  import type { JSX } from "solid-js";
2
2
  import { createNativeBooleanControl } from "./Checkbox.tsx";
3
- import type { CheckboxProps } from "./Checkbox.tsx";
3
+ import type { BooleanControlBaseProps, BooleanControlName } from "./Checkbox.tsx";
4
4
 
5
- export type SwitchProps = Omit<CheckboxProps, "indeterminate">;
5
+ export type SwitchProps = Omit<BooleanControlBaseProps, "indeterminate"> & BooleanControlName;
6
6
 
7
7
  export function Switch(props: SwitchProps): JSX.Element {
8
8
  return createNativeBooleanControl(props, "switch");
@@ -0,0 +1,60 @@
1
+ import * as Primitive from "#sheen-kobalte/tooltip";
2
+ import type { TooltipTriggerRenderProps } from "#sheen-kobalte/tooltip";
3
+ import { onCleanup, splitProps } from "solid-js";
4
+ import type { JSX } from "solid-js";
5
+ import { createTooltipLayer, TooltipLayer } from "./tooltip-layer.tsx";
6
+ import { useShortcutLabel } from "./ShortcutProvider.tsx";
7
+
8
+ /** Spread onto the one focusable element that owns the tooltip. */
9
+ export interface TooltipTriggerBindings {
10
+ readonly ref: (element: HTMLElement) => void;
11
+ readonly "aria-describedby": string | undefined;
12
+ }
13
+
14
+ export interface TooltipTriggerProps {
15
+ content: string;
16
+ shortcut?: string;
17
+ tooltipDisabled?: boolean;
18
+ placement?: "top" | "bottom" | "left" | "right";
19
+ openDelay?: number;
20
+ closeDelay?: number;
21
+ "aria-describedby"?: string;
22
+ children: (trigger: TooltipTriggerBindings) => JSX.Element;
23
+ }
24
+
25
+ function callHandler(handler: unknown, event: Event): void {
26
+ if (typeof handler === "function") handler(event);
27
+ else if (Array.isArray(handler) && typeof handler[0] === "function") handler[0](handler[1], event);
28
+ }
29
+
30
+ /** Hover/focus help attached to an existing focusable element, without rendering another control. */
31
+ export function TooltipTrigger(props: TooltipTriggerProps): JSX.Element {
32
+ const [local] = splitProps(props, ["content", "shortcut", "tooltipDisabled", "placement", "openDelay", "closeDelay", "aria-describedby", "children"]);
33
+ let trigger: HTMLElement | undefined;
34
+ const state = createTooltipLayer(() => trigger, () => local.tooltipDisabled ?? false);
35
+ const shortcutLabel = useShortcutLabel(() => local.shortcut);
36
+ // Primitive handlers attach natively so the element keeps its own JSX event handlers.
37
+ const Target = (primitive: TooltipTriggerRenderProps): JSX.Element => {
38
+ const listeners: readonly (readonly [string, (event: Event) => void])[] = [
39
+ ["pointerenter", event => callHandler(primitive.onPointerEnter, event)], ["pointerleave", event => callHandler(primitive.onPointerLeave, event)],
40
+ ["pointerdown", event => callHandler(primitive.onPointerDown, event)], ["click", event => callHandler(primitive.onClick, event)],
41
+ ["focus", event => callHandler(primitive.onFocus, event)], ["blur", event => callHandler(primitive.onBlur, event)],
42
+ ];
43
+ const detach = () => { for (const [type, listener] of listeners) trigger?.removeEventListener(type, listener); };
44
+ onCleanup(detach);
45
+ return local.children({
46
+ ref: element => {
47
+ detach();
48
+ trigger = element;
49
+ for (const [type, listener] of listeners) element.addEventListener(type, listener);
50
+ if (typeof primitive.ref === "function") primitive.ref(element);
51
+ },
52
+ get "aria-describedby"() { return [local["aria-describedby"], state.open() ? `${state.id}-content` : undefined].filter(Boolean).join(" ") || undefined; },
53
+ });
54
+ };
55
+ return <Primitive.Root open={state.open()} onOpenChange={state.change} disabled={local.tooltipDisabled ?? false}
56
+ placement={local.placement ?? "top"} openDelay={local.openDelay ?? 500} closeDelay={local.closeDelay ?? 100} gutter={6}>
57
+ <Primitive.Trigger as={Target} aria-describedby={undefined} />
58
+ <TooltipLayer state={state} content={local.content} shortcut={shortcutLabel} />
59
+ </Primitive.Root>;
60
+ }
@@ -119,8 +119,10 @@
119
119
  .sheen-checkbox { display: grid; gap: var(--sheen-space-block-xs); }
120
120
  .sheen-checkbox[hidden] { display: none; }
121
121
  .sheen-checkbox-row { display: flex; align-items: center; gap: var(--sheen-space-inline-md); min-block-size: var(--sheen-control-h-md); position: relative; }
122
- .sheen-checkbox-input, .sheen-switch-input { position: absolute; inline-size: var(--sheen-icon-size-md); block-size: var(--sheen-icon-size-md); margin: 0; opacity: 0; pointer-events: none; }
123
- .sheen-checkbox-control { display: grid; place-items: center; flex: 0 0 auto; box-sizing: border-box; inline-size: var(--sheen-icon-size-md); block-size: var(--sheen-icon-size-md); border: var(--sheen-control-border-width) solid var(--sheen-color-border-control); border-radius: var(--sheen-control-radius); background: var(--sheen-color-bg-inset); transition: background-color var(--sheen-duration-fast) var(--sheen-ease-standard), border-color var(--sheen-duration-fast) var(--sheen-ease-standard); }
122
+ .sheen-checkbox-input, .sheen-switch-input { position: absolute; inline-size: var(--sheen-icon-size-md); block-size: var(--sheen-icon-size-md); margin: 0; opacity: 0; cursor: pointer; }
123
+ .sheen-switch-input { inline-size: calc(var(--sheen-icon-size-md) * 2); block-size: calc(var(--sheen-icon-size-md) + var(--sheen-space-inline-xs)); }
124
+ :is(.sheen-checkbox-input, .sheen-switch-input):disabled { cursor: default; }
125
+ .sheen-checkbox-control { pointer-events: none; display: grid; place-items: center; flex: 0 0 auto; box-sizing: border-box; inline-size: var(--sheen-icon-size-md); block-size: var(--sheen-icon-size-md); border: var(--sheen-control-border-width) solid var(--sheen-color-border-control); border-radius: var(--sheen-control-radius); background: var(--sheen-color-bg-inset); transition: background-color var(--sheen-duration-fast) var(--sheen-ease-standard), border-color var(--sheen-duration-fast) var(--sheen-ease-standard); }
124
126
  :is(.sheen-checkbox-row, .sheen-radio-row):not(:hover, :focus-within, :active) :is(.sheen-checkbox-control, .sheen-radio-control) { transition-property: none; }
125
127
  .sheen-checkbox-row:hover .sheen-checkbox-input:not(:disabled, :checked) + .sheen-checkbox-control, .sheen-radio-row:hover .sheen-radio-input:not(:disabled, :checked) + .sheen-radio-control, .sheen-switch-row:hover .sheen-switch-input:not(:disabled, :checked) + .sheen-switch-control { border-color: var(--sheen-color-fg-muted); }
126
128
  .sheen-switch-row:not(:hover, :focus-within, :active) :is(.sheen-switch-control, .sheen-switch-thumb) { transition-property: none; }
@@ -138,7 +140,7 @@
138
140
  .sheen-switch { display: grid; gap: var(--sheen-space-block-xs); }
139
141
  .sheen-switch[hidden] { display: none; }
140
142
  .sheen-switch-row { display: flex; align-items: center; gap: var(--sheen-space-inline-md); min-block-size: var(--sheen-control-h-md); position: relative; }
141
- .sheen-switch-control { display: flex; align-items: center; flex: 0 0 auto; box-sizing: border-box; inline-size: calc(var(--sheen-icon-size-md) * 2); block-size: calc(var(--sheen-icon-size-md) + var(--sheen-space-inline-xs)); padding: var(--sheen-control-border-width); border: var(--sheen-control-border-width) solid var(--sheen-color-border-control); border-radius: calc(var(--sheen-control-radius) * 100); background: var(--sheen-color-bg-inset); transition: background-color var(--sheen-duration-normal) var(--sheen-ease-standard), border-color var(--sheen-duration-fast) var(--sheen-ease-standard); --sheen-switch-travel: calc(var(--sheen-icon-size-md) - var(--sheen-space-inline-xs)); }
143
+ .sheen-switch-control { pointer-events: none; display: flex; align-items: center; flex: 0 0 auto; box-sizing: border-box; inline-size: calc(var(--sheen-icon-size-md) * 2); block-size: calc(var(--sheen-icon-size-md) + var(--sheen-space-inline-xs)); padding: var(--sheen-control-border-width); border: var(--sheen-control-border-width) solid var(--sheen-color-border-control); border-radius: calc(var(--sheen-control-radius) * 100); background: var(--sheen-color-bg-inset); transition: background-color var(--sheen-duration-normal) var(--sheen-ease-standard), border-color var(--sheen-duration-fast) var(--sheen-ease-standard); --sheen-switch-travel: calc(var(--sheen-icon-size-md) - var(--sheen-space-inline-xs)); }
142
144
  .sheen-switch-control:dir(rtl) { --sheen-switch-travel: calc(var(--sheen-space-inline-xs) - var(--sheen-icon-size-md)); }
143
145
  .sheen-switch-thumb { display: block; block-size: 100%; aspect-ratio: 1; border-radius: calc(var(--sheen-control-radius) * 100); background: var(--sheen-color-fg-muted); box-shadow: 0 1px 2px #00000040; transition: translate var(--sheen-duration-normal) var(--sheen-ease-spring), background-color var(--sheen-duration-fast) var(--sheen-ease-standard), scale var(--sheen-duration-press) var(--sheen-ease-standard); }
144
146
  .sheen-switch-row:active .sheen-switch-input:not(:disabled) + .sheen-switch-control .sheen-switch-thumb { scale: 0.9; }
@@ -8,6 +8,9 @@
8
8
  .sheen-command-palette [cmdk-list] { max-block-size: min(26rem, 60vh); overflow: auto; padding: var(--sheen-space-block-sm); scroll-padding-block: var(--sheen-space-block-sm); }
9
9
  .sheen-command-palette [cmdk-group-heading] { padding-block: var(--sheen-space-block-sm) var(--sheen-space-block-xs); padding-inline: var(--sheen-space-inline-sm); color: var(--sheen-color-fg-muted); font-size: var(--sheen-text-ui-sm-size); font-weight: 600; }
10
10
  .sheen-command-palette [cmdk-item] { display: flex; align-items: center; justify-content: space-between; gap: var(--sheen-space-inline-md); min-block-size: var(--sheen-control-h-md); padding-inline: var(--sheen-space-inline-sm); border-radius: var(--sheen-control-radius); cursor: default; }
11
+ .sheen-command-palette .sheen-command-icon { display: inline-flex; flex: none; align-items: center; color: var(--sheen-color-fg-muted); }
12
+ .sheen-command-palette .sheen-command-label { flex: 1 1 auto; min-inline-size: 0; overflow-wrap: anywhere; }
13
+ .sheen-command-palette .sheen-command-trailing { display: inline-flex; flex: none; align-items: center; gap: var(--sheen-space-inline-xs); color: var(--sheen-color-fg-muted); }
11
14
  .sheen-command-palette [cmdk-item][data-selected="true"] { background: var(--sheen-color-bg-selected); }
12
15
  .sheen-command-palette [cmdk-item][data-disabled="true"] { color: var(--sheen-color-fg-subtle); }
13
16
  .sheen-command-palette [cmdk-empty], .sheen-command-palette [cmdk-loading], .sheen-command-palette [role="alert"] { padding: var(--sheen-space-block-lg) var(--sheen-space-inline-sm); color: var(--sheen-color-fg-muted); text-align: center; }