@rcarls/rc-common 0.1.0 → 0.3.0
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.
- package/README.md +43 -3
- package/dist/rc-common.js +493 -152
- package/dist/rc-common.umd.cjs +13 -13
- package/dist/types/ItemsCollectionController.d.ts +100 -0
- package/dist/types/KeyboardInteractionDirective.d.ts +32 -0
- package/dist/types/KeyboardInteractionDirective.test.d.ts +1 -0
- package/dist/types/KeyboardNavigationDirective.d.ts +3 -10
- package/dist/types/MutationObserverController.d.ts +30 -0
- package/dist/types/MutationObserverController.test.d.ts +1 -0
- package/dist/types/RafScheduler.d.ts +16 -0
- package/dist/types/RafScheduler.test.d.ts +1 -0
- package/dist/types/TypeaheadController.d.ts +31 -0
- package/dist/types/TypeaheadController.test.d.ts +1 -0
- package/dist/types/findNearestScrollAncestor.d.ts +9 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/nativeChild.d.ts +54 -0
- package/dist/types/nativeChild.test.d.ts +1 -0
- package/package.json +6 -8
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { ReactiveController, ReactiveControllerHost } from 'lit';
|
|
2
|
+
export interface ItemsCollectionBaseOption {
|
|
3
|
+
value: string;
|
|
4
|
+
label: string;
|
|
5
|
+
disabled?: boolean;
|
|
6
|
+
data?: unknown;
|
|
7
|
+
}
|
|
8
|
+
export interface ItemsCollectionSelectableOption extends ItemsCollectionBaseOption {
|
|
9
|
+
kind?: 'option';
|
|
10
|
+
}
|
|
11
|
+
export interface ItemsCollectionActionOption extends ItemsCollectionBaseOption {
|
|
12
|
+
kind: 'action';
|
|
13
|
+
action: string;
|
|
14
|
+
}
|
|
15
|
+
export type ItemsCollectionOption = ItemsCollectionSelectableOption | ItemsCollectionActionOption;
|
|
16
|
+
export type ItemsCollectionFilterStrategy = 'prefix' | 'contains' | ((label: string, query: string) => boolean);
|
|
17
|
+
export interface ItemsCollectionControllerOptions {
|
|
18
|
+
/** Stable ID prefix for rendered option elements, e.g. `'rc-lb-1'`. */
|
|
19
|
+
idPrefix: string;
|
|
20
|
+
/**
|
|
21
|
+
* Called once on first connect when pre-rendered `<li>` children are found
|
|
22
|
+
* and `setOptions()` has not yet been called. Use to sync selection state
|
|
23
|
+
* after bootstrap.
|
|
24
|
+
*/
|
|
25
|
+
onInitFromDom?: (options: ItemsCollectionOption[]) => void;
|
|
26
|
+
/** Called when the user activates an option by pointer. */
|
|
27
|
+
onActivate: (option: ItemsCollectionOption) => void;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Manages a `<ul role="presentation">` + `<li role="option">` DOM subtree
|
|
31
|
+
* inside the host element's light DOM.
|
|
32
|
+
*
|
|
33
|
+
* Options and selection state are driven imperatively; the controller syncs
|
|
34
|
+
* changes directly to the DOM without triggering Lit re-renders.
|
|
35
|
+
*
|
|
36
|
+
* On first connect the controller can bootstrap from pre-rendered `<li>`
|
|
37
|
+
* elements, enabling progressive enhancement. Once `setOptions()` is called
|
|
38
|
+
* at any point the DOM is fully controlled and bootstrap will not run again.
|
|
39
|
+
*/
|
|
40
|
+
export declare class ItemsCollectionController implements ReactiveController {
|
|
41
|
+
private readonly _host;
|
|
42
|
+
private readonly _idPrefix;
|
|
43
|
+
private readonly _onInitFromDom?;
|
|
44
|
+
private readonly _onActivate;
|
|
45
|
+
private _ul;
|
|
46
|
+
private _options;
|
|
47
|
+
private _liElements;
|
|
48
|
+
private _createLi;
|
|
49
|
+
private _createOption;
|
|
50
|
+
private _selectedValues;
|
|
51
|
+
private _filterText;
|
|
52
|
+
private _filterStrategy;
|
|
53
|
+
private _checkmark;
|
|
54
|
+
private _optionsInitialized;
|
|
55
|
+
constructor(host: ReactiveControllerHost & Element, options: ItemsCollectionControllerOptions);
|
|
56
|
+
hostConnected(): void;
|
|
57
|
+
hostDisconnected(): void;
|
|
58
|
+
get allOptions(): readonly ItemsCollectionOption[];
|
|
59
|
+
get selectedValues(): string[];
|
|
60
|
+
get filterText(): string;
|
|
61
|
+
isSelected(value: string): boolean;
|
|
62
|
+
optionForElement(element: Element): ItemsCollectionOption | null;
|
|
63
|
+
/**
|
|
64
|
+
* Navigable option elements: visible (not `[hidden]`) and not `aria-disabled`.
|
|
65
|
+
* Includes the create option when set. Consumed by `ActiveDescendantController`.
|
|
66
|
+
*/
|
|
67
|
+
get navigableItems(): Element[];
|
|
68
|
+
set checkmark(value: boolean);
|
|
69
|
+
/** Replace the full option list. Sets the controller as authoritative, preventing DOM bootstrap. */
|
|
70
|
+
setOptions(options: ItemsCollectionOption[]): void;
|
|
71
|
+
/** Append a single option without replacing the list. */
|
|
72
|
+
appendOption(opt: ItemsCollectionOption): void;
|
|
73
|
+
/** Update `aria-selected` on all option elements to match `values`. */
|
|
74
|
+
setSelectedValues(values: string[]): void;
|
|
75
|
+
/**
|
|
76
|
+
* Apply a filter — sets `[hidden]` on non-matching `<li>` elements.
|
|
77
|
+
* The strategy is stored and re-used when options change.
|
|
78
|
+
*/
|
|
79
|
+
filterOptions(text: string, strategy?: ItemsCollectionFilterStrategy): void;
|
|
80
|
+
/** Remove `[hidden]` from all option elements. */
|
|
81
|
+
clearFilter(): void;
|
|
82
|
+
/**
|
|
83
|
+
* Show or hide the synthetic "Create" option at the end of the list.
|
|
84
|
+
* Pass `null` to remove it.
|
|
85
|
+
*/
|
|
86
|
+
setCreateOption(label: string | null): void;
|
|
87
|
+
private _ensureContainer;
|
|
88
|
+
private _bootstrapFromDom;
|
|
89
|
+
private _syncOptionsToDom;
|
|
90
|
+
private _createLiElement;
|
|
91
|
+
private _setLabelText;
|
|
92
|
+
private _ensureCheckmark;
|
|
93
|
+
private _removeCheckmark;
|
|
94
|
+
private _syncCheckmarks;
|
|
95
|
+
private _syncSelectedToDom;
|
|
96
|
+
private _syncFilterToDom;
|
|
97
|
+
private _applyFilterToLi;
|
|
98
|
+
private _isVisible;
|
|
99
|
+
private _onPointerDown;
|
|
100
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { PartInfo, ElementPart } from 'lit/directive.js';
|
|
2
|
+
import { AsyncDirective } from 'lit/async-directive.js';
|
|
3
|
+
/**
|
|
4
|
+
* Options for the keyInteraction directive.
|
|
5
|
+
*/
|
|
6
|
+
export interface KeyInteractionOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Element to set/remove `data-interaction-mode` on.
|
|
9
|
+
* Defaults to the element the directive is applied to.
|
|
10
|
+
* Pass the LitElement host (`this`) when the attribute needs to be visible
|
|
11
|
+
* to external CSS (e.g. for `::part()` theming via rc-theme-material).
|
|
12
|
+
*/
|
|
13
|
+
attributeTarget?: Element;
|
|
14
|
+
}
|
|
15
|
+
declare class KeyboardInteractionDirective extends AsyncDirective {
|
|
16
|
+
private _element?;
|
|
17
|
+
private _options;
|
|
18
|
+
private _isPointerFocus;
|
|
19
|
+
private _onPointerdown;
|
|
20
|
+
private _onFocusin;
|
|
21
|
+
private _onKeydown;
|
|
22
|
+
private get _target();
|
|
23
|
+
constructor(partInfo: PartInfo);
|
|
24
|
+
protected _init(): void;
|
|
25
|
+
render(_options?: KeyInteractionOptions): symbol;
|
|
26
|
+
update(part: ElementPart, [options]: Parameters<this['render']>): void;
|
|
27
|
+
disconnected(): void;
|
|
28
|
+
reconnected(): void;
|
|
29
|
+
}
|
|
30
|
+
export declare const keyInteraction: (_options?: KeyInteractionOptions | undefined) => import('lit/directive.js').DirectiveResult<typeof KeyboardInteractionDirective>;
|
|
31
|
+
export type { KeyboardInteractionDirective };
|
|
32
|
+
export default keyInteraction;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,15 +1,10 @@
|
|
|
1
1
|
import { PartInfo, ElementPart } from 'lit/directive.js';
|
|
2
2
|
import { AsyncDirective } from 'lit/async-directive.js';
|
|
3
|
-
export type KeyboardNavigationAction = 'next' | 'prev' | 'start' | 'end' | 'open-to-first' | 'open-to-last' | 'escape' | 'activate' | 'toggle';
|
|
3
|
+
export type KeyboardNavigationAction = 'next' | 'prev' | 'next-large' | 'prev-large' | 'start' | 'end' | 'open-to-first' | 'open-to-last' | 'escape' | 'activate' | 'toggle';
|
|
4
4
|
/**
|
|
5
5
|
* Options for the keyNavigation directive.
|
|
6
6
|
*/
|
|
7
7
|
export interface KeyNavigationOptions {
|
|
8
|
-
/**
|
|
9
|
-
* Set data-interaction-mode attribute for keyboard vs mouse styling.
|
|
10
|
-
* @default true
|
|
11
|
-
*/
|
|
12
|
-
useInteractionModeAttr?: boolean;
|
|
13
8
|
/**
|
|
14
9
|
* Dispatch 'escape' action on Escape key.
|
|
15
10
|
* @default false
|
|
@@ -46,7 +41,6 @@ export interface KeyNavigationOptions {
|
|
|
46
41
|
declare class KeyboardNavigationDirective extends AsyncDirective {
|
|
47
42
|
private _element?;
|
|
48
43
|
private _keyDownHandle;
|
|
49
|
-
private _mouseClickHandle;
|
|
50
44
|
private _callback;
|
|
51
45
|
private _options;
|
|
52
46
|
/**
|
|
@@ -58,13 +52,12 @@ declare class KeyboardNavigationDirective extends AsyncDirective {
|
|
|
58
52
|
constructor(partInfo: PartInfo);
|
|
59
53
|
private _normalizeKey;
|
|
60
54
|
protected _onKeydown(e: KeyboardEvent): void;
|
|
61
|
-
protected _onMouseClick(_e: MouseEvent): void;
|
|
62
55
|
protected _init(): void;
|
|
63
56
|
/**
|
|
64
57
|
* @param _cb - Callback function invoked with the navigation action
|
|
65
|
-
* @param _options - Options object or deprecated boolean
|
|
58
|
+
* @param _options - Options object or deprecated boolean (deprecated).
|
|
66
59
|
* @deprecated Passing a boolean as the second parameter is deprecated.
|
|
67
|
-
* Use an options object instead
|
|
60
|
+
* Use an options object instead.
|
|
68
61
|
*/
|
|
69
62
|
render(_cb: (action: KeyboardNavigationAction) => void, _options?: KeyNavigationOptions | boolean): symbol;
|
|
70
63
|
update(part: ElementPart, [cb, optionsOrBoolean]: Parameters<this['render']>): void;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ReactiveController, ReactiveControllerHost } from 'lit';
|
|
2
|
+
export interface MutationObserverControllerOptions {
|
|
3
|
+
/** Node to observe. Accepts a node reference or getter. */
|
|
4
|
+
target: Node | (() => Node | null) | null;
|
|
5
|
+
/** Native observer callback. */
|
|
6
|
+
callback: MutationCallback;
|
|
7
|
+
/** Native observer options. Defaults to `{ childList: true }`. */
|
|
8
|
+
observerOptions?: MutationObserverInit;
|
|
9
|
+
/** When true, disconnects and stays inert. */
|
|
10
|
+
disabled?: boolean;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Lifecycle-safe wrapper around `MutationObserver`.
|
|
14
|
+
*
|
|
15
|
+
* A null target is silently inert; call `setOptions()` when the target or
|
|
16
|
+
* observer options change.
|
|
17
|
+
*/
|
|
18
|
+
export declare class MutationObserverController implements ReactiveController {
|
|
19
|
+
private _attached;
|
|
20
|
+
private _observer;
|
|
21
|
+
private _options;
|
|
22
|
+
constructor(host: ReactiveControllerHost, options: MutationObserverControllerOptions);
|
|
23
|
+
setOptions(next: Partial<MutationObserverControllerOptions>): void;
|
|
24
|
+
hostConnected(): void;
|
|
25
|
+
hostDisconnected(): void;
|
|
26
|
+
private _resolveTarget;
|
|
27
|
+
private _attach;
|
|
28
|
+
private _detach;
|
|
29
|
+
}
|
|
30
|
+
export default MutationObserverController;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ReactiveController, ReactiveControllerHost } from 'lit';
|
|
2
|
+
export type RafSchedulerCallback = (timestamp: DOMHighResTimeStamp) => void;
|
|
3
|
+
/**
|
|
4
|
+
* Coalesces repeated work into one `requestAnimationFrame` callback and
|
|
5
|
+
* cancels pending work when the host disconnects.
|
|
6
|
+
*/
|
|
7
|
+
export declare class RafScheduler implements ReactiveController {
|
|
8
|
+
private _callback;
|
|
9
|
+
private _frame;
|
|
10
|
+
constructor(host?: ReactiveControllerHost);
|
|
11
|
+
get pending(): boolean;
|
|
12
|
+
schedule(callback: RafSchedulerCallback): void;
|
|
13
|
+
cancel(): void;
|
|
14
|
+
hostDisconnected(): void;
|
|
15
|
+
}
|
|
16
|
+
export default RafScheduler;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export interface TypeaheadControllerOptions<T> {
|
|
2
|
+
/** Items searched by typeahead. */
|
|
3
|
+
items: () => readonly T[];
|
|
4
|
+
/** Text used for matching each item. */
|
|
5
|
+
text: (item: T) => string;
|
|
6
|
+
/** Called with the matched item and index. */
|
|
7
|
+
onMatch: (item: T, index: number) => void;
|
|
8
|
+
/** Search start index for the next key. Defaults to `0`. */
|
|
9
|
+
startIndex?: () => number;
|
|
10
|
+
/** Buffer clear delay in milliseconds. Defaults to `500`. */
|
|
11
|
+
timeout?: number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Small APG-style typeahead buffer for composite widgets.
|
|
15
|
+
*
|
|
16
|
+
* It handles printable-key buffering and timeout reset; consumers keep control
|
|
17
|
+
* of focus, active descendant, and selection behavior.
|
|
18
|
+
*/
|
|
19
|
+
export declare class TypeaheadController<T> {
|
|
20
|
+
private _buffer;
|
|
21
|
+
private _timer;
|
|
22
|
+
private readonly _options;
|
|
23
|
+
constructor(options: TypeaheadControllerOptions<T>);
|
|
24
|
+
get buffer(): string;
|
|
25
|
+
reset(): void;
|
|
26
|
+
handleKeydown(event: KeyboardEvent): boolean;
|
|
27
|
+
search(char: string): T | null;
|
|
28
|
+
private _scheduleReset;
|
|
29
|
+
private _clearTimer;
|
|
30
|
+
}
|
|
31
|
+
export default TypeaheadController;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Walks up the DOM from `el` and returns the first ancestor whose computed
|
|
3
|
+
* `overflow` or `overflow-y` is `auto` or `scroll`. Falls back to
|
|
4
|
+
* `document.scrollingElement` (the root scroller) when no ancestor qualifies.
|
|
5
|
+
*
|
|
6
|
+
* Start the walk from `el.parentElement` when the element itself should not
|
|
7
|
+
* be considered (e.g. a FAB looking for its containing scroll context).
|
|
8
|
+
*/
|
|
9
|
+
export declare function findNearestScrollAncestor(el: Element): Element;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
export * from './ActiveDescendantController';
|
|
2
|
+
export * from './ItemsCollectionController';
|
|
3
|
+
export * from './KeyboardInteractionDirective';
|
|
2
4
|
export * from './KeyboardNavigationDirective';
|
|
3
5
|
export * from './MouseMoveDirective';
|
|
4
6
|
export * from './DragController';
|
|
5
7
|
export * from './ResizeController';
|
|
6
8
|
export * from './AnchorController';
|
|
7
9
|
export * from './ScrollObserverController';
|
|
10
|
+
export * from './MutationObserverController';
|
|
11
|
+
export * from './RafScheduler';
|
|
12
|
+
export * from './TypeaheadController';
|
|
13
|
+
export * from './findNearestScrollAncestor';
|
|
8
14
|
export * from './isFocusable';
|
|
15
|
+
export * from './nativeChild';
|
|
9
16
|
export * from './RovingTabIndexMixin';
|
|
10
17
|
export * from './sliderUtils';
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { ReactiveController, ReactiveControllerHost } from 'lit';
|
|
2
|
+
export type NativeChildHost = ReactiveControllerHost & Element;
|
|
3
|
+
export interface MissingDirectChildWarningOptions {
|
|
4
|
+
/** Selector scoped to direct children, such as `:scope > button`. */
|
|
5
|
+
selector: string;
|
|
6
|
+
/** Minimum matching child count required before no warning is emitted. Defaults to `1`. */
|
|
7
|
+
minimum?: number;
|
|
8
|
+
/** Human-readable child description used by the default warning text. */
|
|
9
|
+
childDescription?: string;
|
|
10
|
+
/** Component name used by the default warning text. Defaults to `host.localName`. */
|
|
11
|
+
hostName?: string;
|
|
12
|
+
/** Full warning message. Overrides the generated message. */
|
|
13
|
+
message?: string;
|
|
14
|
+
}
|
|
15
|
+
export interface NativeChildControllerOptions<E extends Element> {
|
|
16
|
+
/** Selector scoped to the host's light DOM. Prefer `:scope > ...`. */
|
|
17
|
+
selector: string;
|
|
18
|
+
/** Called when the matched child changes. */
|
|
19
|
+
onChange?: (child: E | null, previous: E | null) => void;
|
|
20
|
+
/** Called when no child is found during a sync pass. */
|
|
21
|
+
onMissing?: (host: NativeChildHost) => void;
|
|
22
|
+
/**
|
|
23
|
+
* Observe light-DOM child changes and re-sync automatically. Defaults to
|
|
24
|
+
* `false`; leave off for static direct-child checks.
|
|
25
|
+
*/
|
|
26
|
+
observe?: boolean | MutationObserverInit;
|
|
27
|
+
}
|
|
28
|
+
/** Returns the first matching child under `host`, or `null` when absent. */
|
|
29
|
+
export declare function getDirectChild<E extends Element = Element>(host: ParentNode, selector: string): E | null;
|
|
30
|
+
/** Returns all matching children under `host`. */
|
|
31
|
+
export declare function getDirectChildren<E extends Element = Element>(host: ParentNode, selector: string): E[];
|
|
32
|
+
/**
|
|
33
|
+
* Emits a standard direct-child warning when the expected child is absent.
|
|
34
|
+
* Call this from a DEV-guarded component lifecycle hook.
|
|
35
|
+
*/
|
|
36
|
+
export declare function warnMissingDirectChild(host: Element, options: MissingDirectChildWarningOptions): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Tracks a native light-DOM child and re-runs setup when it is replaced.
|
|
39
|
+
* Useful for progressive-enhancement wrappers that wire listeners to a
|
|
40
|
+
* consumer-provided native control.
|
|
41
|
+
*/
|
|
42
|
+
export declare class NativeChildController<E extends Element = Element> implements ReactiveController {
|
|
43
|
+
private _observer;
|
|
44
|
+
private _child;
|
|
45
|
+
private readonly _host;
|
|
46
|
+
private readonly _options;
|
|
47
|
+
constructor(host: NativeChildHost, options: NativeChildControllerOptions<E>);
|
|
48
|
+
get child(): E | null;
|
|
49
|
+
hostConnected(): void;
|
|
50
|
+
hostDisconnected(): void;
|
|
51
|
+
/** Re-read the child from the host and invoke `onChange` when it changes. */
|
|
52
|
+
sync(): E | null;
|
|
53
|
+
}
|
|
54
|
+
export default NativeChildController;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -3,14 +3,14 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "0.
|
|
7
|
-
"description": "Shared
|
|
6
|
+
"version": "0.3.0",
|
|
7
|
+
"description": "Shared controllers, directives, and DOM utilities for rc-webcomponents.",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
10
10
|
"url": "git+https://github.com/richardcarls/rc-webcomponents.git",
|
|
11
11
|
"directory": "packages/rc-common"
|
|
12
12
|
},
|
|
13
|
-
"homepage": "https://github.
|
|
13
|
+
"homepage": "https://richardcarls.github.io/rc-webcomponents/",
|
|
14
14
|
"license": "MIT",
|
|
15
15
|
"type": "module",
|
|
16
16
|
"files": [
|
|
@@ -33,13 +33,11 @@
|
|
|
33
33
|
"types": "./dist/types/index.d.ts",
|
|
34
34
|
"sideEffects": false,
|
|
35
35
|
"scripts": {
|
|
36
|
-
"dev": "vite",
|
|
37
36
|
"build": "tsc && vite build",
|
|
38
37
|
"preview": "vite preview",
|
|
39
|
-
"test:browser": "vitest",
|
|
40
|
-
"test:browser:chrome": "vitest --project=chromium",
|
|
41
|
-
"test:browser:firefox": "vitest --project=firefox"
|
|
42
|
-
"yalc:publish": "yalc publish --push"
|
|
38
|
+
"test:browser": "vitest --run",
|
|
39
|
+
"test:browser:chrome": "vitest --run --project=chromium",
|
|
40
|
+
"test:browser:firefox": "vitest --run --project=firefox"
|
|
43
41
|
},
|
|
44
42
|
"dependencies": {
|
|
45
43
|
"@oddbird/css-anchor-positioning": "^0.7.0"
|