You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
390 lines
11 KiB
390 lines
11 KiB
import { untrack } from 'svelte';
|
|
import { createAttrs } from '$uix/morfo';
|
|
import { context, type WithRefOpts } from '../../provider';
|
|
import { type ActiveProps, type StateProps } from '$libs/reactive';
|
|
import type { Orientation, Direction, SomaKeyboardEvent, SomaMouseEvent } from '../../types';
|
|
import { KEYS, getDirectionalKeys } from '../../keyboard';
|
|
|
|
import { toolbarMorfo } from '../../../morfo/components/toolbar';
|
|
import { Soma } from '../../core/soma.svelte';
|
|
import type { SomaRuntime, SomaRuntimePart } from '../../runtime.svelte';
|
|
import type { ToolbarGroupType } from './types';
|
|
|
|
const attrs = createAttrs(toolbarMorfo);
|
|
|
|
// ── Root ─────────────────────────────────────────────────────────────────────
|
|
|
|
interface ToolbarOpts
|
|
extends
|
|
WithRefOpts,
|
|
ActiveProps<{
|
|
orientation: Orientation;
|
|
loop: boolean;
|
|
dir: Direction;
|
|
}> {}
|
|
|
|
export class ToolbarProvider {
|
|
readonly opts: ToolbarOpts;
|
|
readonly runtimePart: SomaRuntimePart;
|
|
readonly soma: Soma;
|
|
readonly runtime: SomaRuntime;
|
|
static readonly ctx = context<ToolbarProvider>('Toolbar');
|
|
static get(): ToolbarProvider | undefined {
|
|
return this.ctx.getOr(undefined) as ToolbarProvider | undefined;
|
|
}
|
|
static require(): ToolbarProvider {
|
|
return this.ctx.get();
|
|
}
|
|
|
|
static create(opts: ToolbarOpts) {
|
|
return new ToolbarProvider(opts);
|
|
}
|
|
|
|
// Track last focused element for roving tabindex
|
|
lastFocusedElement = $state<HTMLElement | null>(null);
|
|
|
|
private constructor(opts: ToolbarOpts) {
|
|
this.opts = opts;
|
|
this.soma = Soma.require();
|
|
this.runtime = this.soma.runtime(toolbarMorfo, {
|
|
props: {
|
|
orientation: () => this.opts.orientation.current
|
|
}
|
|
});
|
|
this.runtimePart = this.runtime.part('provider', {
|
|
id: opts.id,
|
|
ref: opts.ref,
|
|
owner: this,
|
|
context: ToolbarProvider.ctx,
|
|
syncAttrs: true
|
|
});
|
|
}
|
|
|
|
/** Get all focusable items in the toolbar, scoped to this root. */
|
|
getItems(): HTMLElement[] {
|
|
const root = this.opts.ref?.current;
|
|
if (!root) return [];
|
|
return Array.from(
|
|
root.querySelectorAll<HTMLElement>(
|
|
`[${attrs.button}]:not([disabled]), [${attrs.link}], [${attrs['group-item']}]:not([disabled])`
|
|
)
|
|
).filter((el) => el.closest(`[${attrs.provider}]`) === root);
|
|
}
|
|
|
|
/**
|
|
* Whether `el` should hold `tabindex=0` under the roving policy.
|
|
*
|
|
* Reactive only on `lastFocusedElement`. The first-in-DOM-order fallback
|
|
* runs via `untrack` to avoid pulling the whole DOM read into the caller's
|
|
* dependency set — otherwise any per-item `props` $derived that calls
|
|
* `isTabStop` would invalidate when any sibling re-renders, cascading into
|
|
* an effect update loop (see A30/A31 in COMPONENT_GUIDE).
|
|
*/
|
|
isTabStop(el: HTMLElement | null): boolean {
|
|
if (!el) return false;
|
|
if (this.lastFocusedElement) return this.lastFocusedElement === el;
|
|
return untrack(() => this.getItems()[0] === el);
|
|
}
|
|
|
|
readonly onkeydown = (e: SomaKeyboardEvent<HTMLElement>) => {
|
|
const orientation = this.opts.orientation.current;
|
|
const dir = this.opts.dir.current;
|
|
const { nextKey, prevKey } = getDirectionalKeys(dir, orientation);
|
|
const items = this.getItems();
|
|
if (items.length === 0) return;
|
|
|
|
const activeEl = this.soma.dom.activeElement(this.opts.ref.current) as HTMLElement | null;
|
|
const currentIndex = activeEl ? items.indexOf(activeEl) : -1;
|
|
if (currentIndex < 0) return;
|
|
|
|
const loop = this.opts.loop.current;
|
|
let targetIndex = -1;
|
|
|
|
if (e.key === nextKey) {
|
|
e.preventDefault();
|
|
targetIndex = loop
|
|
? (currentIndex + 1) % items.length
|
|
: Math.min(currentIndex + 1, items.length - 1);
|
|
} else if (e.key === prevKey) {
|
|
e.preventDefault();
|
|
targetIndex = loop
|
|
? (currentIndex - 1 + items.length) % items.length
|
|
: Math.max(currentIndex - 1, 0);
|
|
} else if (e.key === KEYS.HOME) {
|
|
e.preventDefault();
|
|
targetIndex = 0;
|
|
} else if (e.key === KEYS.END) {
|
|
e.preventDefault();
|
|
targetIndex = items.length - 1;
|
|
}
|
|
|
|
if (targetIndex >= 0 && items[targetIndex]) {
|
|
this.soma.dom.focus(items[targetIndex]);
|
|
this.lastFocusedElement = items[targetIndex];
|
|
}
|
|
};
|
|
|
|
readonly onfocusin = (e: FocusEvent) => {
|
|
if (e.target instanceof HTMLElement) {
|
|
this.lastFocusedElement = e.target;
|
|
}
|
|
};
|
|
|
|
readonly props = $derived.by(() =>
|
|
this.runtimePart.assert({
|
|
...this.runtimePart.props,
|
|
dir: this.opts.dir.current,
|
|
onkeydown: this.onkeydown,
|
|
onfocusin: this.onfocusin
|
|
} as const)
|
|
);
|
|
}
|
|
|
|
// ── Button ───────────────────────────────────────────────────────────────────
|
|
|
|
interface ToolbarButtonOpts
|
|
extends
|
|
WithRefOpts,
|
|
ActiveProps<{
|
|
disabled: boolean;
|
|
}> {}
|
|
|
|
export class ToolbarButtonProvider {
|
|
readonly opts: ToolbarButtonOpts;
|
|
readonly runtimePart: SomaRuntimePart;
|
|
static create(opts: ToolbarButtonOpts) {
|
|
return new ToolbarButtonProvider(opts);
|
|
}
|
|
|
|
readonly provider: ToolbarProvider;
|
|
|
|
private constructor(opts: ToolbarButtonOpts) {
|
|
this.opts = opts;
|
|
this.provider = ToolbarProvider.require();
|
|
this.runtimePart = this.provider.runtime.part('button', {
|
|
id: opts.id,
|
|
ref: opts.ref,
|
|
props: {
|
|
disabled: () => this.opts.disabled.current,
|
|
orientation: () => this.provider.opts.orientation.current
|
|
},
|
|
owner: this,
|
|
syncAttrs: true
|
|
});
|
|
}
|
|
|
|
readonly props = $derived.by(() =>
|
|
this.runtimePart.assert({
|
|
...this.runtimePart.props,
|
|
tabindex: this.provider.isTabStop(this.opts.ref.current) ? 0 : -1,
|
|
disabled: this.opts.disabled.current || undefined
|
|
} as const)
|
|
);
|
|
}
|
|
|
|
// ── Link ─────────────────────────────────────────────────────────────────────
|
|
|
|
interface ToolbarLinkOpts extends WithRefOpts {}
|
|
|
|
export class ToolbarLinkProvider {
|
|
readonly opts: ToolbarLinkOpts;
|
|
readonly runtimePart: SomaRuntimePart;
|
|
static create(opts: ToolbarLinkOpts) {
|
|
return new ToolbarLinkProvider(opts);
|
|
}
|
|
|
|
readonly provider: ToolbarProvider;
|
|
|
|
private constructor(opts: ToolbarLinkOpts) {
|
|
this.opts = opts;
|
|
this.provider = ToolbarProvider.require();
|
|
this.runtimePart = this.provider.runtime.part('link', {
|
|
id: opts.id,
|
|
ref: opts.ref,
|
|
props: {
|
|
orientation: () => this.provider.opts.orientation.current
|
|
},
|
|
owner: this,
|
|
syncAttrs: true
|
|
});
|
|
}
|
|
|
|
readonly props = $derived.by(() =>
|
|
this.runtimePart.assert({
|
|
...this.runtimePart.props,
|
|
tabindex: this.provider.isTabStop(this.opts.ref.current) ? 0 : -1
|
|
} as const)
|
|
);
|
|
}
|
|
|
|
// ── Group (ToggleGroup inside toolbar) ───────────────────────────────────────
|
|
|
|
interface ToolbarGroupOpts
|
|
extends
|
|
WithRefOpts,
|
|
StateProps<{ value: string[] }>,
|
|
ActiveProps<{
|
|
type: ToolbarGroupType;
|
|
disabled: boolean;
|
|
}> {}
|
|
|
|
export class ToolbarGroupProvider {
|
|
readonly opts: ToolbarGroupOpts;
|
|
readonly runtimePart: SomaRuntimePart;
|
|
static readonly ctx = context<ToolbarGroupProvider>('ToolbarGroup');
|
|
static get(): ToolbarGroupProvider | undefined {
|
|
return this.ctx.getOr(undefined) as ToolbarGroupProvider | undefined;
|
|
}
|
|
static require(): ToolbarGroupProvider {
|
|
return this.ctx.get();
|
|
}
|
|
|
|
static create(opts: ToolbarGroupOpts) {
|
|
return new ToolbarGroupProvider(opts);
|
|
}
|
|
|
|
readonly provider: ToolbarProvider;
|
|
|
|
private constructor(opts: ToolbarGroupOpts) {
|
|
this.opts = opts;
|
|
this.provider = ToolbarProvider.require();
|
|
this.runtimePart = this.provider.runtime.part('group', {
|
|
id: opts.id,
|
|
ref: opts.ref,
|
|
props: {
|
|
disabled: () => this.opts.disabled.current,
|
|
orientation: () => this.provider.opts.orientation.current
|
|
},
|
|
owner: this,
|
|
context: ToolbarGroupProvider.ctx,
|
|
syncAttrs: true
|
|
});
|
|
}
|
|
|
|
isItemPressed(value: string): boolean {
|
|
return this.opts.value.current.includes(value);
|
|
}
|
|
|
|
toggleItem(value: string) {
|
|
if (this.opts.disabled.current) return;
|
|
|
|
const current = this.opts.value.current;
|
|
const isPressed = current.includes(value);
|
|
|
|
let next: string[];
|
|
if (this.opts.type.current === 'single') {
|
|
next = isPressed ? [] : [value];
|
|
} else {
|
|
next = isPressed ? current.filter((v) => v !== value) : [...current, value];
|
|
}
|
|
|
|
this.opts.value.current = next;
|
|
}
|
|
|
|
readonly props = $derived.by(() =>
|
|
this.runtimePart.assert({
|
|
...this.runtimePart.props
|
|
} as const)
|
|
);
|
|
}
|
|
|
|
// ── GroupItem ─────────────────────────────────────────────────────────────────
|
|
|
|
interface ToolbarGroupItemOpts
|
|
extends
|
|
WithRefOpts,
|
|
ActiveProps<{
|
|
value: string;
|
|
disabled: boolean;
|
|
}> {}
|
|
|
|
export class ToolbarGroupItemProvider {
|
|
readonly opts: ToolbarGroupItemOpts;
|
|
readonly runtimePart: SomaRuntimePart;
|
|
static create(opts: ToolbarGroupItemOpts) {
|
|
return new ToolbarGroupItemProvider(opts);
|
|
}
|
|
|
|
readonly group: ToolbarGroupProvider;
|
|
readonly provider: ToolbarProvider;
|
|
|
|
private constructor(opts: ToolbarGroupItemOpts) {
|
|
this.opts = opts;
|
|
this.group = ToolbarGroupProvider.require();
|
|
this.provider = this.group.provider;
|
|
this.runtimePart = this.provider.runtime.part('group-item', {
|
|
id: opts.id,
|
|
ref: opts.ref,
|
|
states: {
|
|
on: () => this.isPressed
|
|
},
|
|
props: {
|
|
disabled: () => this.isDisabled,
|
|
orientation: () => this.provider.opts.orientation.current
|
|
},
|
|
owner: this,
|
|
syncAttrs: true
|
|
});
|
|
}
|
|
|
|
readonly isPressed = $derived.by(() => this.group.isItemPressed(this.opts.value.current));
|
|
|
|
readonly isDisabled = $derived.by(
|
|
() => this.opts.disabled.current || this.group.opts.disabled.current
|
|
);
|
|
|
|
readonly onclick = (_e: SomaMouseEvent<HTMLButtonElement>) => {
|
|
if (this.isDisabled) return;
|
|
this.group.toggleItem(this.opts.value.current);
|
|
};
|
|
|
|
readonly props = $derived.by(() =>
|
|
this.runtimePart.assert({
|
|
...this.runtimePart.props,
|
|
tabindex: this.provider.isTabStop(this.opts.ref.current) ? 0 : -1,
|
|
'data-value': this.opts.value.current,
|
|
disabled: this.isDisabled || undefined,
|
|
onclick: this.onclick
|
|
} as const)
|
|
);
|
|
}
|
|
|
|
// ── Separator ────────────────────────────────────────────────────────────────
|
|
|
|
interface ToolbarSeparatorOpts extends WithRefOpts {}
|
|
|
|
export class ToolbarSeparatorProvider {
|
|
readonly opts: ToolbarSeparatorOpts;
|
|
readonly runtimePart: SomaRuntimePart;
|
|
static create(opts: ToolbarSeparatorOpts) {
|
|
return new ToolbarSeparatorProvider(opts);
|
|
}
|
|
|
|
readonly provider: ToolbarProvider;
|
|
|
|
private constructor(opts: ToolbarSeparatorOpts) {
|
|
this.opts = opts;
|
|
this.provider = ToolbarProvider.require();
|
|
this.runtimePart = this.provider.runtime.part('separator', {
|
|
id: opts.id,
|
|
ref: opts.ref,
|
|
props: {
|
|
orientation: () => this.separatorOrientation
|
|
},
|
|
owner: this,
|
|
syncAttrs: true
|
|
});
|
|
}
|
|
|
|
/** Separator orientation is perpendicular to toolbar orientation. */
|
|
private get separatorOrientation(): Orientation {
|
|
return this.provider.opts.orientation.current === 'horizontal' ? 'vertical' : 'horizontal';
|
|
}
|
|
|
|
readonly props = $derived.by(() =>
|
|
this.runtimePart.assert({
|
|
...this.runtimePart.props
|
|
} as const)
|
|
);
|
|
}
|