@openartifactsai/ui 0.0.0-stage → 0.23.0-rc.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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +5 -2
- package/package.json +82 -4
- package/src/button.d.ts +30 -0
- package/src/button.js +29 -0
- package/src/clicks.d.ts +63 -0
- package/src/clicks.js +75 -0
- package/src/cn.d.ts +3 -0
- package/src/cn.js +5 -0
- package/src/color-picker.d.ts +98 -0
- package/src/color-picker.js +395 -0
- package/src/color.d.ts +150 -0
- package/src/color.js +543 -0
- package/src/crash-boundary.d.ts +29 -0
- package/src/crash-boundary.js +32 -0
- package/src/data/google-fonts.json +1775 -0
- package/src/drag-cover.d.ts +48 -0
- package/src/drag-cover.js +41 -0
- package/src/effect-editors.d.ts +48 -0
- package/src/effect-editors.js +230 -0
- package/src/effects.d.ts +118 -0
- package/src/effects.js +425 -0
- package/src/escape-to-popup.d.ts +53 -0
- package/src/escape-to-popup.js +78 -0
- package/src/field-announce.d.ts +28 -0
- package/src/field-announce.js +43 -0
- package/src/fields.d.ts +176 -0
- package/src/fields.js +377 -0
- package/src/focusables.d.ts +50 -0
- package/src/focusables.js +61 -0
- package/src/font-picker.d.ts +62 -0
- package/src/font-picker.js +242 -0
- package/src/fonts.d.ts +121 -0
- package/src/fonts.js +273 -0
- package/src/history-diff.d.ts +64 -0
- package/src/history-diff.js +151 -0
- package/src/history.d.ts +95 -0
- package/src/history.js +160 -0
- package/src/index.d.ts +38 -0
- package/src/index.js +38 -0
- package/src/key-chord.d.ts +74 -0
- package/src/key-chord.js +228 -0
- package/src/keys.d.ts +142 -0
- package/src/keys.js +172 -0
- package/src/layout.d.ts +74 -0
- package/src/layout.js +173 -0
- package/src/link-card-view.d.ts +97 -0
- package/src/link-card-view.js +175 -0
- package/src/link-card.d.ts +136 -0
- package/src/link-card.js +158 -0
- package/src/menu-shortcuts.d.ts +31 -0
- package/src/menu-shortcuts.js +91 -0
- package/src/menu.d.ts +120 -0
- package/src/menu.js +100 -0
- package/src/number-entry.d.ts +140 -0
- package/src/number-entry.js +202 -0
- package/src/number-limits.d.ts +2 -0
- package/src/number-limits.js +2 -0
- package/src/number-wheel.d.ts +29 -0
- package/src/number-wheel.js +27 -0
- package/src/numbers.d.ts +28 -0
- package/src/numbers.js +124 -0
- package/src/overlays.d.ts +71 -0
- package/src/overlays.js +131 -0
- package/src/pressed.d.ts +5 -0
- package/src/pressed.js +10 -0
- package/src/scroll.d.ts +24 -0
- package/src/scroll.js +111 -0
- package/src/scrub-number-field.d.ts +79 -0
- package/src/scrub-number-field.js +254 -0
- package/src/select-announce.d.ts +50 -0
- package/src/select-announce.js +71 -0
- package/src/slider-session.d.ts +43 -0
- package/src/slider-session.js +39 -0
- package/src/styles.css +96 -0
- package/src/tab-strip-layout.d.ts +76 -0
- package/src/tab-strip-layout.js +111 -0
- package/src/tab-strip.d.ts +73 -0
- package/src/tab-strip.js +498 -0
- package/src/text-area-view.d.ts +35 -0
- package/src/text-area-view.js +142 -0
- package/src/text-area.d.ts +38 -0
- package/src/text-area.js +51 -0
- package/src/theme.css +34 -0
- package/src/toast-fit.d.ts +21 -0
- package/src/toast-fit.js +30 -0
- package/src/toast-tone.d.ts +27 -0
- package/src/toast-tone.js +22 -0
- package/src/tooltip.d.ts +23 -0
- package/src/tooltip.js +18 -0
package/src/menu.d.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import type { BaseUIEvent } from '@base-ui/react/types';
|
|
2
|
+
import { type KeyboardEvent, type ReactElement, type ReactNode, type WheelEvent } from 'react';
|
|
3
|
+
import { type KeyChord } from './key-chord.js';
|
|
4
|
+
import { MENU_TREE_ATTRIBUTE } from './menu-shortcuts.js';
|
|
5
|
+
/** Shared popup look of menus, popovers and selects. */
|
|
6
|
+
export declare const POPUP_CLASS = "rounded-lg bg-surface p-1 text-[13px] text-fg shadow-popup outline-none origin-[var(--transform-origin)] transition-[opacity,transform] duration-100 data-[starting-style]:opacity-0 data-[starting-style]:scale-95 data-[ending-style]:opacity-0";
|
|
7
|
+
/** Shared look of a menu's rows. */
|
|
8
|
+
export declare const MENU_ITEM_CLASS = "flex h-7 min-w-40 cursor-default select-none items-center gap-2 rounded-md px-2 outline-none data-[highlighted]:bg-hover data-[disabled]:opacity-40";
|
|
9
|
+
/** The props of a menu popup that acts on its rows' chords. */
|
|
10
|
+
export interface MenuShortcutPopupProps {
|
|
11
|
+
[MENU_TREE_ATTRIBUTE]: string;
|
|
12
|
+
onKeyDown: (event: BaseUIEvent<KeyboardEvent<HTMLDivElement>>) => void;
|
|
13
|
+
}
|
|
14
|
+
/** The props a menu popup takes to act on its rows' chords; a submenu joins the tree of the menu it opens from. */
|
|
15
|
+
export declare function useMenuShortcutPopup(): {
|
|
16
|
+
tree: string;
|
|
17
|
+
popupProps: MenuShortcutPopupProps;
|
|
18
|
+
};
|
|
19
|
+
/** Provides a menu tree's id to its submenus. */
|
|
20
|
+
export declare const MenuTreeProvider: import("react").Provider<string | null>;
|
|
21
|
+
/**
|
|
22
|
+
* The chord parts of a menu row: the shown label (hidden from screen readers, which read the chord's words instead),
|
|
23
|
+
* `aria-keyshortcuts`, and the mark that lets the open menu press the row.
|
|
24
|
+
*/
|
|
25
|
+
export declare function menuChordParts(keys: KeyChord | undefined, hintOnly?: boolean): {
|
|
26
|
+
label: string;
|
|
27
|
+
spoken: string;
|
|
28
|
+
rowProps: Record<string, string>;
|
|
29
|
+
} | null;
|
|
30
|
+
/** Props of {@link Menu}. */
|
|
31
|
+
export interface MenuProps {
|
|
32
|
+
/** The element that opens the menu (rendered via Base UI's `render`). */
|
|
33
|
+
trigger: ReactElement<Record<string, unknown>>;
|
|
34
|
+
children: ReactNode;
|
|
35
|
+
side?: 'top' | 'bottom' | 'left' | 'right';
|
|
36
|
+
align?: 'start' | 'center' | 'end';
|
|
37
|
+
open?: boolean;
|
|
38
|
+
onOpenChange?: (open: boolean) => void;
|
|
39
|
+
/**
|
|
40
|
+
* Whether the open menu holds the page (outside clicks and scrolling blocked, focus kept in it). Left out: as a
|
|
41
|
+
* menu does by default.
|
|
42
|
+
*/
|
|
43
|
+
modal?: boolean;
|
|
44
|
+
/** A point in the viewport (px) to open at, as a menu opened by a right-click does; left out: by the trigger. */
|
|
45
|
+
at?: {
|
|
46
|
+
x: number;
|
|
47
|
+
y: number;
|
|
48
|
+
};
|
|
49
|
+
/** A wheel turned over the open menu. */
|
|
50
|
+
onPopupWheel?: (event: WheelEvent<HTMLDivElement>) => void;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* A dropdown menu. Base UI gives it keyboard navigation and typeahead (a
|
|
54
|
+
* Space typed mid-search is part of the search). A held Enter or Space
|
|
55
|
+
* presses the trigger and each row once.
|
|
56
|
+
*/
|
|
57
|
+
export declare function Menu({ trigger, children, side, align, open, onOpenChange, modal, at, onPopupWheel }: MenuProps): import("react/jsx-runtime").JSX.Element;
|
|
58
|
+
/** Props of {@link MenuItem}. */
|
|
59
|
+
export interface MenuItemProps {
|
|
60
|
+
children: ReactNode;
|
|
61
|
+
onSelect?: () => void;
|
|
62
|
+
icon?: ReactNode;
|
|
63
|
+
/** Text shown at the end of the row (display only). */
|
|
64
|
+
shortcut?: string;
|
|
65
|
+
/** The row's key chord: shown at the end of the row, announced, and pressed while the menu is open. */
|
|
66
|
+
keys?: KeyChord;
|
|
67
|
+
/** Shows and announces `keys` without the open menu acting on them: the key goes where the focus was. */
|
|
68
|
+
hintOnly?: boolean;
|
|
69
|
+
disabled?: boolean;
|
|
70
|
+
danger?: boolean;
|
|
71
|
+
/** Shown on hover: why a disabled row is unavailable, for one. */
|
|
72
|
+
title?: string;
|
|
73
|
+
}
|
|
74
|
+
/** One action row. The rest of a double click on it does not reach what the closed menu uncovers. */
|
|
75
|
+
export declare function MenuItem({ children, onSelect, icon, shortcut, keys, hintOnly, disabled, danger, title }: MenuItemProps): import("react/jsx-runtime").JSX.Element;
|
|
76
|
+
/** Props of {@link MenuLinkItem}. */
|
|
77
|
+
export interface MenuLinkItemProps {
|
|
78
|
+
children: ReactNode;
|
|
79
|
+
href: string;
|
|
80
|
+
icon?: ReactNode;
|
|
81
|
+
/** Where it opens; `_blank` for a new tab. */
|
|
82
|
+
target?: string;
|
|
83
|
+
/** The link's `rel` (e.g. `noopener noreferrer nofollow` for another site's address). */
|
|
84
|
+
rel?: string;
|
|
85
|
+
}
|
|
86
|
+
/** One row that is a link (an `<a>`: it opens, middle-clicks and copies as a link does). The menu closes on it. */
|
|
87
|
+
export declare function MenuLinkItem({ children, href, icon, target, rel }: MenuLinkItemProps): import("react/jsx-runtime").JSX.Element;
|
|
88
|
+
/** A toggle row with a check mark. */
|
|
89
|
+
export declare function MenuCheckboxItem({ children, checked, onCheckedChange, closeOnClick, }: {
|
|
90
|
+
children: ReactNode;
|
|
91
|
+
checked: boolean;
|
|
92
|
+
onCheckedChange: (checked: boolean) => void;
|
|
93
|
+
/** Close the menu on click (for a pick among options rather than a toggle). */
|
|
94
|
+
closeOnClick?: boolean;
|
|
95
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
96
|
+
/**
|
|
97
|
+
* An on/off row: its label, then a switch that shows the state (the row itself is the checkbox, so the switch is
|
|
98
|
+
* only drawn). The menu stays open unless `closeOnClick`.
|
|
99
|
+
*/
|
|
100
|
+
export declare function MenuSwitchItem({ children, checked, onCheckedChange, icon, disabled, closeOnClick, }: {
|
|
101
|
+
children: ReactNode;
|
|
102
|
+
checked: boolean;
|
|
103
|
+
onCheckedChange: (checked: boolean) => void;
|
|
104
|
+
icon?: ReactNode;
|
|
105
|
+
disabled?: boolean;
|
|
106
|
+
/** Close the menu on click (when the click asks something rather than flips the switch). */
|
|
107
|
+
closeOnClick?: boolean;
|
|
108
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
109
|
+
/** A thin rule between groups of items. */
|
|
110
|
+
export declare function MenuSeparator(): import("react/jsx-runtime").JSX.Element;
|
|
111
|
+
/** A non-interactive heading or note inside a menu. */
|
|
112
|
+
export declare function MenuLabel({ children }: {
|
|
113
|
+
children: ReactNode;
|
|
114
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
115
|
+
/** A nested menu opened from a row. */
|
|
116
|
+
export declare function MenuSubmenu({ label, icon, children }: {
|
|
117
|
+
label: ReactNode;
|
|
118
|
+
icon?: ReactNode;
|
|
119
|
+
children: ReactNode;
|
|
120
|
+
}): import("react/jsx-runtime").JSX.Element;
|
package/src/menu.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { Menu as BaseMenu } from '@base-ui/react/menu';
|
|
3
|
+
import { Check, ChevronRight } from 'lucide-react';
|
|
4
|
+
import { createContext, useContext, useId, useMemo } from 'react';
|
|
5
|
+
import { flushSync } from 'react-dom';
|
|
6
|
+
import { swallowFollowUpClicks } from "./clicks.js";
|
|
7
|
+
import { cn } from "./cn.js";
|
|
8
|
+
import { useEscapeHandoffRef } from "./escape-to-popup.js";
|
|
9
|
+
import { chordAriaKeys, chordLabel, chordSpoken, holdChord, menuTakesChord, onApplePlatform } from "./key-chord.js";
|
|
10
|
+
import { MENU_SHORTCUT_ATTRIBUTE, MENU_TREE_ATTRIBUTE, pressMenuShortcut } from "./menu-shortcuts.js";
|
|
11
|
+
import { preventRepeatedActivation } from "./keys.js";
|
|
12
|
+
/** Shared popup look of menus, popovers and selects. */
|
|
13
|
+
export const POPUP_CLASS = 'rounded-lg bg-surface p-1 text-[13px] text-fg shadow-popup outline-none origin-[var(--transform-origin)] transition-[opacity,transform] duration-100 data-[starting-style]:opacity-0 data-[starting-style]:scale-95 data-[ending-style]:opacity-0';
|
|
14
|
+
/** Shared look of a menu's rows. */
|
|
15
|
+
export const MENU_ITEM_CLASS = 'flex h-7 min-w-40 cursor-default select-none items-center gap-2 rounded-md px-2 outline-none data-[highlighted]:bg-hover data-[disabled]:opacity-40';
|
|
16
|
+
/** A row with a check mark: its label takes up to two lines, the mark stays by the first. */
|
|
17
|
+
const CHECK_ITEM_CLASS = 'flex min-h-7 min-w-40 cursor-default select-none items-start gap-2 rounded-md px-2 py-1 leading-5 outline-none data-[highlighted]:bg-hover data-[disabled]:opacity-40';
|
|
18
|
+
const MenuTreeContext = createContext(null);
|
|
19
|
+
/** The props a menu popup takes to act on its rows' chords; a submenu joins the tree of the menu it opens from. */
|
|
20
|
+
export function useMenuShortcutPopup() {
|
|
21
|
+
const parent = useContext(MenuTreeContext);
|
|
22
|
+
const own = useId();
|
|
23
|
+
const tree = parent ?? own;
|
|
24
|
+
return {
|
|
25
|
+
tree,
|
|
26
|
+
popupProps: {
|
|
27
|
+
[MENU_TREE_ATTRIBUTE]: tree,
|
|
28
|
+
onKeyDown: (event) => pressMenuShortcut(event, flushSync),
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/** Provides a menu tree's id to its submenus. */
|
|
33
|
+
export const MenuTreeProvider = MenuTreeContext.Provider;
|
|
34
|
+
/**
|
|
35
|
+
* The chord parts of a menu row: the shown label (hidden from screen readers, which read the chord's words instead),
|
|
36
|
+
* `aria-keyshortcuts`, and the mark that lets the open menu press the row.
|
|
37
|
+
*/
|
|
38
|
+
export function menuChordParts(keys, hintOnly = false) {
|
|
39
|
+
if (keys === undefined)
|
|
40
|
+
return null;
|
|
41
|
+
const apple = onApplePlatform();
|
|
42
|
+
const rowProps = { 'aria-keyshortcuts': chordAriaKeys(keys, apple) };
|
|
43
|
+
if (!hintOnly && menuTakesChord(holdChord(keys, apple), apple))
|
|
44
|
+
rowProps[MENU_SHORTCUT_ATTRIBUTE] = '';
|
|
45
|
+
return { label: chordLabel(keys, apple), spoken: chordSpoken(keys, apple), rowProps };
|
|
46
|
+
}
|
|
47
|
+
/** A spot with no size at `at`, for the menu to open from. */
|
|
48
|
+
function pointAnchor(at) {
|
|
49
|
+
return { getBoundingClientRect: () => new DOMRect(at.x, at.y, 0, 0) };
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* A dropdown menu. Base UI gives it keyboard navigation and typeahead (a
|
|
53
|
+
* Space typed mid-search is part of the search). A held Enter or Space
|
|
54
|
+
* presses the trigger and each row once.
|
|
55
|
+
*/
|
|
56
|
+
export function Menu({ trigger, children, side = 'bottom', align = 'start', open, onOpenChange, modal, at, onPopupWheel }) {
|
|
57
|
+
const handoffRef = useEscapeHandoffRef();
|
|
58
|
+
const { tree, popupProps } = useMenuShortcutPopup();
|
|
59
|
+
const x = at?.x;
|
|
60
|
+
const y = at?.y;
|
|
61
|
+
const anchor = useMemo(() => (x === undefined || y === undefined ? undefined : pointAnchor({ x, y })), [x, y]);
|
|
62
|
+
return (_jsxs(BaseMenu.Root, { open: open, onOpenChange: onOpenChange ? (next) => onOpenChange(next) : undefined, ...(modal === undefined ? {} : { modal }), children: [_jsx(BaseMenu.Trigger, { render: trigger, onKeyDown: preventRepeatedActivation }), _jsx(BaseMenu.Portal, { children: _jsx(BaseMenu.Positioner, { side: side, align: align, sideOffset: anchor === undefined ? 6 : 2, className: "z-[1000]", ...(anchor === undefined ? {} : { anchor }), children: _jsx(BaseMenu.Popup, { ref: handoffRef, className: POPUP_CLASS, ...popupProps, ...(onPopupWheel === undefined ? {} : { onWheel: onPopupWheel }), children: _jsx(MenuTreeProvider, { value: tree, children: children }) }) }) })] }));
|
|
63
|
+
}
|
|
64
|
+
/** One action row. The rest of a double click on it does not reach what the closed menu uncovers. */
|
|
65
|
+
export function MenuItem({ children, onSelect, icon, shortcut, keys, hintOnly, disabled, danger, title }) {
|
|
66
|
+
const chord = menuChordParts(keys, hintOnly);
|
|
67
|
+
return (_jsxs(BaseMenu.Item, { className: cn(MENU_ITEM_CLASS, danger && 'text-danger'), disabled: disabled, title: title, onClick: (event) => {
|
|
68
|
+
swallowFollowUpClicks(event.nativeEvent);
|
|
69
|
+
onSelect?.();
|
|
70
|
+
}, onKeyDown: preventRepeatedActivation, ...chord?.rowProps, children: [_jsx("span", { className: "flex size-4 items-center justify-center text-fg-muted", children: icon }), _jsx("span", { className: "min-w-0 flex-1", children: children }), chord ? (_jsxs("span", { className: "ml-4 text-xs text-fg-subtle", children: [_jsx("span", { "aria-hidden": "true", children: chord.label }), _jsx("span", { className: "sr-only", children: chord.spoken })] })) : shortcut ? (_jsx("span", { className: "ml-4 text-xs text-fg-subtle", children: shortcut })) : null] }));
|
|
71
|
+
}
|
|
72
|
+
/** One row that is a link (an `<a>`: it opens, middle-clicks and copies as a link does). The menu closes on it. */
|
|
73
|
+
export function MenuLinkItem({ children, href, icon, target, rel }) {
|
|
74
|
+
return (_jsxs(BaseMenu.LinkItem, { className: MENU_ITEM_CLASS, href: href, target: target, rel: rel, closeOnClick: true, children: [_jsx("span", { className: "flex size-4 items-center justify-center text-fg-muted", children: icon }), _jsx("span", { className: "min-w-0 flex-1", children: children })] }));
|
|
75
|
+
}
|
|
76
|
+
/** A toggle row with a check mark. */
|
|
77
|
+
export function MenuCheckboxItem({ children, checked, onCheckedChange, closeOnClick = false, }) {
|
|
78
|
+
return (_jsxs(BaseMenu.CheckboxItem, { className: CHECK_ITEM_CLASS, checked: checked, closeOnClick: closeOnClick, onCheckedChange: (next) => onCheckedChange(next), onKeyDown: preventRepeatedActivation, children: [_jsx("span", { className: "flex h-5 w-4 shrink-0 items-center justify-center", children: _jsx(BaseMenu.CheckboxItemIndicator, { children: _jsx(Check, { className: "size-3.5" }) }) }), _jsx("span", { className: "line-clamp-2 min-w-0 flex-1 break-words", children: children })] }));
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* An on/off row: its label, then a switch that shows the state (the row itself is the checkbox, so the switch is
|
|
82
|
+
* only drawn). The menu stays open unless `closeOnClick`.
|
|
83
|
+
*/
|
|
84
|
+
export function MenuSwitchItem({ children, checked, onCheckedChange, icon, disabled, closeOnClick = false, }) {
|
|
85
|
+
return (_jsxs(BaseMenu.CheckboxItem, { className: MENU_ITEM_CLASS, checked: checked, disabled: disabled, closeOnClick: closeOnClick, onCheckedChange: (next) => onCheckedChange(next), onKeyDown: preventRepeatedActivation, children: [_jsx("span", { className: "flex size-4 items-center justify-center text-fg-muted", children: icon }), _jsx("span", { className: "flex-1", children: children }), _jsx("span", { "aria-hidden": "true", "data-checked": checked ? '' : undefined, className: "relative ml-4 inline-flex h-4 w-7 shrink-0 items-center rounded-full bg-line-strong p-0.5 transition-colors data-[checked]:bg-accent", children: _jsx("span", { className: cn('size-3 rounded-full bg-white shadow transition-transform', checked && 'translate-x-3') }) })] }));
|
|
86
|
+
}
|
|
87
|
+
/** A thin rule between groups of items. */
|
|
88
|
+
export function MenuSeparator() {
|
|
89
|
+
return _jsx(BaseMenu.Separator, { className: "mx-1 my-1 h-px bg-line" });
|
|
90
|
+
}
|
|
91
|
+
/** A non-interactive heading or note inside a menu. */
|
|
92
|
+
export function MenuLabel({ children }) {
|
|
93
|
+
return _jsx("div", { className: "px-2 py-1 text-xs text-fg-subtle", children: children });
|
|
94
|
+
}
|
|
95
|
+
/** A nested menu opened from a row. */
|
|
96
|
+
export function MenuSubmenu({ label, icon, children }) {
|
|
97
|
+
const handoffRef = useEscapeHandoffRef();
|
|
98
|
+
const { popupProps } = useMenuShortcutPopup();
|
|
99
|
+
return (_jsxs(BaseMenu.SubmenuRoot, { children: [_jsxs(BaseMenu.SubmenuTrigger, { className: MENU_ITEM_CLASS, onKeyDown: preventRepeatedActivation, children: [_jsx("span", { className: "flex size-4 items-center justify-center text-fg-muted", children: icon }), _jsx("span", { className: "flex-1", children: label }), _jsx(ChevronRight, { className: "size-3.5 text-fg-subtle" })] }), _jsx(BaseMenu.Portal, { children: _jsx(BaseMenu.Positioner, { sideOffset: 4, className: "z-[1000]", children: _jsx(BaseMenu.Popup, { ref: handoffRef, className: POPUP_CLASS, ...popupProps, children: children }) }) })] }));
|
|
100
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a number field does with what is typed or pasted into it, and what it
|
|
3
|
+
* says about it. One set of rules and words for every editor's number fields:
|
|
4
|
+
*
|
|
5
|
+
* - a value past a bound is held at the bound;
|
|
6
|
+
* - a field whose minimum is zero or more takes no negative numbers: it drops
|
|
7
|
+
* a leading minus sign and keeps the number (`-5` is 5, not the minimum);
|
|
8
|
+
* - text that is no number changes nothing.
|
|
9
|
+
*
|
|
10
|
+
* A field reports each change as a {@link NumberLimit}; the sentences below
|
|
11
|
+
* ({@link numberLimitMessage}, {@link notANumberMessage}) are what a screen
|
|
12
|
+
* reader hears about them. No DOM.
|
|
13
|
+
*/
|
|
14
|
+
/** A field's bounds. */
|
|
15
|
+
export interface NumberBounds {
|
|
16
|
+
min?: number;
|
|
17
|
+
max?: number;
|
|
18
|
+
}
|
|
19
|
+
/** True for a field that takes no negative numbers. */
|
|
20
|
+
export declare function refusesMinus(bounds: NumberBounds): boolean;
|
|
21
|
+
/** True when a key types a minus sign. */
|
|
22
|
+
export declare function isMinusSign(key: string): boolean;
|
|
23
|
+
/** True when text, read as a number, starts with a minus sign (format characters and leading spaces aside). */
|
|
24
|
+
export declare function startsWithMinus(text: string): boolean;
|
|
25
|
+
/** True when nothing but spaces and format characters comes before `at`: a minus sign typed there leads the number. */
|
|
26
|
+
export declare function leadsNumber(text: string, at: number): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* The number in typed text, or null when it is none. `decimal` is the
|
|
29
|
+
* separator the field writes (`.` or `,`); the other one is taken as
|
|
30
|
+
* grouping. Full-width digits and any minus sign are read too.
|
|
31
|
+
*/
|
|
32
|
+
export declare function readTypedNumber(text: string, decimal?: string): number | null;
|
|
33
|
+
/** `value` held inside the bounds. */
|
|
34
|
+
export declare function withinBounds(value: number, bounds: NumberBounds): number;
|
|
35
|
+
/** Why a field changed what was typed: held at its maximum or minimum, or a minus sign dropped. */
|
|
36
|
+
export type NumberLimitReason = 'max' | 'min' | 'no-minus';
|
|
37
|
+
/** One change a field made to typed text. */
|
|
38
|
+
export interface NumberLimit {
|
|
39
|
+
reason: NumberLimitReason;
|
|
40
|
+
/** The bound the value was held at; for `no-minus`, the field's minimum. */
|
|
41
|
+
limit: number;
|
|
42
|
+
}
|
|
43
|
+
/** What typed text keeps once the field's bounds apply. */
|
|
44
|
+
export interface SettledNumber {
|
|
45
|
+
value: number;
|
|
46
|
+
/** What changed, in the order it happened (minus sign first). */
|
|
47
|
+
limits: NumberLimit[];
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Applies the bounds to a typed number. `leadingMinus` says the text
|
|
51
|
+
* started with a minus sign: a field that takes no negative numbers keeps
|
|
52
|
+
* the number without it (and reports `no-minus`); then a value past a bound
|
|
53
|
+
* is held there (and reports `max` or `min`).
|
|
54
|
+
*/
|
|
55
|
+
export declare function settleNumber(typed: number, leadingMinus: boolean, bounds: NumberBounds): SettledNumber;
|
|
56
|
+
/** What typed text keeps (see {@link settleNumber}); null when the text is no number. */
|
|
57
|
+
export declare function settleTypedText(text: string, bounds: NumberBounds, decimal?: string): SettledNumber | null;
|
|
58
|
+
/**
|
|
59
|
+
* An arrow-key step by `change` from typed text: from the number without a
|
|
60
|
+
* minus sign the field refuses, then held inside the bounds. Null when the
|
|
61
|
+
* text is no number.
|
|
62
|
+
*/
|
|
63
|
+
export declare function stepTypedText(text: string, change: number, bounds: NumberBounds, decimal?: string): SettledNumber | null;
|
|
64
|
+
/**
|
|
65
|
+
* The limits that still hold once a field kept `kept`: a `max` or `min`
|
|
66
|
+
* counts only when the value really ended at that bound (the field may have
|
|
67
|
+
* rounded it elsewhere). A dropped minus sign always counts.
|
|
68
|
+
*/
|
|
69
|
+
export declare function limitsKept(limits: readonly NumberLimit[], kept: number | null): NumberLimit[];
|
|
70
|
+
/** Said when a field that takes no negative numbers drops a minus sign (typed, pasted, or pressed). */
|
|
71
|
+
export declare function minusIgnoredMessage(name?: string): string;
|
|
72
|
+
/** Said when a typed value was held at a bound; `shown` is the bound as the field writes it, with its unit. */
|
|
73
|
+
export declare function heldAtLimitMessage(reason: 'max' | 'min', shown: string, name?: string): string;
|
|
74
|
+
/** Said when the text committed is no number, so nothing changed. */
|
|
75
|
+
export declare function notANumberMessage(name?: string, unit?: string): string;
|
|
76
|
+
/** How a message names the field and writes a value. */
|
|
77
|
+
export interface NumberLimitWording {
|
|
78
|
+
/** The field's name (its accessible name); "This field" without one. */
|
|
79
|
+
name?: string;
|
|
80
|
+
/** The unit written after the value, after a space. */
|
|
81
|
+
unit?: string;
|
|
82
|
+
/** How the field writes a number (default: the locale's plain number format). */
|
|
83
|
+
format?: (value: number) => string;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* What to tell a screen reader about the changes a field made to typed text,
|
|
87
|
+
* one sentence each, in order; empty when there were none. Fields report
|
|
88
|
+
* several changes at once (`-50` in a 0–32 field: the minus sign, then the
|
|
89
|
+
* maximum), and a live region reads only its latest message, so they are said
|
|
90
|
+
* together.
|
|
91
|
+
*/
|
|
92
|
+
export declare function numberLimitMessage(limits: readonly NumberLimit[], wording?: NumberLimitWording): string;
|
|
93
|
+
/** Same as {@link isMinusSign}. */
|
|
94
|
+
export declare const isMinusKey: typeof isMinusSign;
|
|
95
|
+
/** Same as {@link readTypedNumber}. */
|
|
96
|
+
export declare const parseEntry: typeof readTypedNumber;
|
|
97
|
+
/** How a typed value was changed before it was kept. */
|
|
98
|
+
export interface EntryOutcome {
|
|
99
|
+
/** The value kept. */
|
|
100
|
+
value: number;
|
|
101
|
+
/** A minus sign the field refuses was left out. */
|
|
102
|
+
minusDropped: boolean;
|
|
103
|
+
/** The value went past this bound and was held at it. */
|
|
104
|
+
limit: 'max' | 'min' | null;
|
|
105
|
+
}
|
|
106
|
+
/** A {@link SettledNumber} as an {@link EntryOutcome}. */
|
|
107
|
+
export declare function entryOutcome(settled: SettledNumber): EntryOutcome;
|
|
108
|
+
/** What typed or pasted text keeps, as an {@link EntryOutcome}; null when it is no number. */
|
|
109
|
+
export declare function settleEntry(text: string, bounds: NumberBounds, decimal?: string): EntryOutcome | null;
|
|
110
|
+
/** An arrow-key step, as an {@link EntryOutcome} (see {@link stepTypedText}). */
|
|
111
|
+
export declare function stepEntry(text: string, change: number, bounds: NumberBounds, decimal?: string): EntryOutcome | null;
|
|
112
|
+
/** The value of an arrow-key step from text that starts with a minus sign the field refuses; null otherwise. */
|
|
113
|
+
export declare function stepWithoutMinus(text: string, change: number, bounds: NumberBounds, decimal?: string): number | null;
|
|
114
|
+
/** What a number field reports: a refused minus key, a typed value it changed, or text it did not take. */
|
|
115
|
+
export type NumberEntryEvent = {
|
|
116
|
+
kind: 'minus-key';
|
|
117
|
+
} | {
|
|
118
|
+
kind: 'entry';
|
|
119
|
+
outcome: EntryOutcome;
|
|
120
|
+
} | {
|
|
121
|
+
kind: 'not-a-number';
|
|
122
|
+
};
|
|
123
|
+
/** The sentences for each {@link NumberEntryEvent}. */
|
|
124
|
+
export interface NumberEntryWords {
|
|
125
|
+
minusKey(name: string | undefined): string;
|
|
126
|
+
/** Null when the value was kept as typed. `shown` is the kept value as the field shows it (with its unit). */
|
|
127
|
+
changed(outcome: EntryOutcome, name: string | undefined, shown: string): string | null;
|
|
128
|
+
notANumber(name: string | undefined, unit: string | undefined): string;
|
|
129
|
+
}
|
|
130
|
+
/** The words every number field says (the sentences above). */
|
|
131
|
+
export declare const NUMBER_ENTRY_WORDS: NumberEntryWords;
|
|
132
|
+
/** What the field context of a message holds. */
|
|
133
|
+
export interface EntryMessageContext {
|
|
134
|
+
name: string | undefined;
|
|
135
|
+
/** The kept value as the field shows it (with its unit). */
|
|
136
|
+
shown: string;
|
|
137
|
+
unit: string | undefined;
|
|
138
|
+
}
|
|
139
|
+
/** What to say for an event a number field reports; null for nothing. */
|
|
140
|
+
export declare function entryMessage(event: NumberEntryEvent, words: NumberEntryWords, context: EntryMessageContext): string | null;
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a number field does with what is typed or pasted into it, and what it
|
|
3
|
+
* says about it. One set of rules and words for every editor's number fields:
|
|
4
|
+
*
|
|
5
|
+
* - a value past a bound is held at the bound;
|
|
6
|
+
* - a field whose minimum is zero or more takes no negative numbers: it drops
|
|
7
|
+
* a leading minus sign and keeps the number (`-5` is 5, not the minimum);
|
|
8
|
+
* - text that is no number changes nothing.
|
|
9
|
+
*
|
|
10
|
+
* A field reports each change as a {@link NumberLimit}; the sentences below
|
|
11
|
+
* ({@link numberLimitMessage}, {@link notANumberMessage}) are what a screen
|
|
12
|
+
* reader hears about them. No DOM.
|
|
13
|
+
*/
|
|
14
|
+
/** The minus signs a number may start with: ASCII, the typographic minus, full-width, figure dash, en and em dash, small minus. */
|
|
15
|
+
const MINUS_SIGN = /[-−-‒–—﹣]/;
|
|
16
|
+
const MINUS_SIGNS = /[-−-‒–—﹣]/g;
|
|
17
|
+
/** Invisible format characters some keyboards and locales put around numbers. */
|
|
18
|
+
const FORMAT_CHARS = /\p{Cf}/gu;
|
|
19
|
+
/** True for a field that takes no negative numbers. */
|
|
20
|
+
export function refusesMinus(bounds) {
|
|
21
|
+
return bounds.min !== undefined && bounds.min >= 0;
|
|
22
|
+
}
|
|
23
|
+
/** True when a key types a minus sign. */
|
|
24
|
+
export function isMinusSign(key) {
|
|
25
|
+
return key.length === 1 && MINUS_SIGN.test(key);
|
|
26
|
+
}
|
|
27
|
+
/** True when text, read as a number, starts with a minus sign (format characters and leading spaces aside). */
|
|
28
|
+
export function startsWithMinus(text) {
|
|
29
|
+
return MINUS_SIGN.test(text.replace(FORMAT_CHARS, '').trimStart().charAt(0));
|
|
30
|
+
}
|
|
31
|
+
/** True when nothing but spaces and format characters comes before `at`: a minus sign typed there leads the number. */
|
|
32
|
+
export function leadsNumber(text, at) {
|
|
33
|
+
return text.slice(0, at).replace(FORMAT_CHARS, '').trim() === '';
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The number in typed text, or null when it is none. `decimal` is the
|
|
37
|
+
* separator the field writes (`.` or `,`); the other one is taken as
|
|
38
|
+
* grouping. Full-width digits and any minus sign are read too.
|
|
39
|
+
*/
|
|
40
|
+
export function readTypedNumber(text, decimal = '.') {
|
|
41
|
+
const spaced = text.replace(FORMAT_CHARS, '').trim();
|
|
42
|
+
// A space between digits parts two values ("8 16" is a padding), not one number.
|
|
43
|
+
if (/[\d0-9.,.,]\s+[\d0-9.,.,]/.test(spaced))
|
|
44
|
+
return null;
|
|
45
|
+
let plain = spaced
|
|
46
|
+
.replace(/\s+/g, '')
|
|
47
|
+
.replace(/[0-9]/g, (digit) => String(digit.charCodeAt(0) - 0xff10))
|
|
48
|
+
.replace(/./g, '.')
|
|
49
|
+
.replace(/,/g, ',')
|
|
50
|
+
.replace(MINUS_SIGNS, '-')
|
|
51
|
+
.replace(/+/g, '+');
|
|
52
|
+
// The other separator groups thousands, in threes ("1,250.5"); anywhere else ("1,5") it is no number.
|
|
53
|
+
const group = decimal === ',' ? '.' : ',';
|
|
54
|
+
if (plain.includes(group) && !new RegExp(`^[-+]?\\d{1,3}(?:\\${group}\\d{3})+(?:\\${decimal}\\d*)?$`).test(plain))
|
|
55
|
+
return null;
|
|
56
|
+
plain = decimal === ',' ? plain.replace(/\./g, '').replace(',', '.') : plain.replace(/,/g, '');
|
|
57
|
+
if (!/^[-+]?(?:\d+\.?\d*|\.\d+)$/.test(plain))
|
|
58
|
+
return null;
|
|
59
|
+
const value = Number(plain);
|
|
60
|
+
return Number.isFinite(value) ? value : null;
|
|
61
|
+
}
|
|
62
|
+
/** `value` held inside the bounds. */
|
|
63
|
+
export function withinBounds(value, bounds) {
|
|
64
|
+
if (bounds.max !== undefined && value > bounds.max)
|
|
65
|
+
return bounds.max;
|
|
66
|
+
if (bounds.min !== undefined && value < bounds.min)
|
|
67
|
+
return bounds.min;
|
|
68
|
+
return value;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Applies the bounds to a typed number. `leadingMinus` says the text
|
|
72
|
+
* started with a minus sign: a field that takes no negative numbers keeps
|
|
73
|
+
* the number without it (and reports `no-minus`); then a value past a bound
|
|
74
|
+
* is held there (and reports `max` or `min`).
|
|
75
|
+
*/
|
|
76
|
+
export function settleNumber(typed, leadingMinus, bounds) {
|
|
77
|
+
const limits = [];
|
|
78
|
+
let value = typed;
|
|
79
|
+
if (leadingMinus && refusesMinus(bounds)) {
|
|
80
|
+
value = Math.abs(value);
|
|
81
|
+
limits.push({ reason: 'no-minus', limit: bounds.min ?? 0 });
|
|
82
|
+
}
|
|
83
|
+
if (bounds.max !== undefined && value > bounds.max) {
|
|
84
|
+
value = bounds.max;
|
|
85
|
+
limits.push({ reason: 'max', limit: bounds.max });
|
|
86
|
+
}
|
|
87
|
+
else if (bounds.min !== undefined && value < bounds.min) {
|
|
88
|
+
value = bounds.min;
|
|
89
|
+
limits.push({ reason: 'min', limit: bounds.min });
|
|
90
|
+
}
|
|
91
|
+
return { value, limits };
|
|
92
|
+
}
|
|
93
|
+
/** What typed text keeps (see {@link settleNumber}); null when the text is no number. */
|
|
94
|
+
export function settleTypedText(text, bounds, decimal = '.') {
|
|
95
|
+
const typed = readTypedNumber(text, decimal);
|
|
96
|
+
return typed === null ? null : settleNumber(typed, startsWithMinus(text), bounds);
|
|
97
|
+
}
|
|
98
|
+
/** Drops the float noise a step adds (0.1 + 0.2). */
|
|
99
|
+
function roundStep(value) {
|
|
100
|
+
return Math.round(value * 1e6) / 1e6;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* An arrow-key step by `change` from typed text: from the number without a
|
|
104
|
+
* minus sign the field refuses, then held inside the bounds. Null when the
|
|
105
|
+
* text is no number.
|
|
106
|
+
*/
|
|
107
|
+
export function stepTypedText(text, change, bounds, decimal = '.') {
|
|
108
|
+
const typed = readTypedNumber(text, decimal);
|
|
109
|
+
if (typed === null)
|
|
110
|
+
return null;
|
|
111
|
+
const dropped = startsWithMinus(text) && refusesMinus(bounds);
|
|
112
|
+
const held = settleNumber(roundStep((dropped ? Math.abs(typed) : typed) + change), false, bounds);
|
|
113
|
+
return dropped ? { value: held.value, limits: [{ reason: 'no-minus', limit: bounds.min ?? 0 }, ...held.limits] } : held;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* The limits that still hold once a field kept `kept`: a `max` or `min`
|
|
117
|
+
* counts only when the value really ended at that bound (the field may have
|
|
118
|
+
* rounded it elsewhere). A dropped minus sign always counts.
|
|
119
|
+
*/
|
|
120
|
+
export function limitsKept(limits, kept) {
|
|
121
|
+
return limits.filter((limit) => limit.reason === 'no-minus' || limit.limit === kept);
|
|
122
|
+
}
|
|
123
|
+
/* --------------------------------- words ---------------------------------- */
|
|
124
|
+
/** The field's name in a sentence: its accessible name, or "This field". */
|
|
125
|
+
function fieldSubject(name) {
|
|
126
|
+
return name?.trim() || 'This field';
|
|
127
|
+
}
|
|
128
|
+
/** Said when a field that takes no negative numbers drops a minus sign (typed, pasted, or pressed). */
|
|
129
|
+
export function minusIgnoredMessage(name) {
|
|
130
|
+
return `Minus sign ignored. ${fieldSubject(name)} doesn’t take negative numbers.`;
|
|
131
|
+
}
|
|
132
|
+
/** Said when a typed value was held at a bound; `shown` is the bound as the field writes it, with its unit. */
|
|
133
|
+
export function heldAtLimitMessage(reason, shown, name) {
|
|
134
|
+
return `Changed to ${shown}. ${fieldSubject(name)} can’t go ${reason === 'max' ? 'higher' : 'lower'}.`;
|
|
135
|
+
}
|
|
136
|
+
/** Said when the text committed is no number, so nothing changed. */
|
|
137
|
+
export function notANumberMessage(name, unit) {
|
|
138
|
+
return `Nothing changed. ${fieldSubject(name)} takes a number ${unit ? `in ${unit}` : 'only'}.`;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* What to tell a screen reader about the changes a field made to typed text,
|
|
142
|
+
* one sentence each, in order; empty when there were none. Fields report
|
|
143
|
+
* several changes at once (`-50` in a 0–32 field: the minus sign, then the
|
|
144
|
+
* maximum), and a live region reads only its latest message, so they are said
|
|
145
|
+
* together.
|
|
146
|
+
*/
|
|
147
|
+
export function numberLimitMessage(limits, wording = {}) {
|
|
148
|
+
const format = wording.format ?? ((value) => new Intl.NumberFormat().format(value));
|
|
149
|
+
const shown = (value) => (wording.unit ? `${format(value)} ${wording.unit}` : format(value));
|
|
150
|
+
return limits
|
|
151
|
+
.map((limit) => (limit.reason === 'no-minus' ? minusIgnoredMessage(wording.name) : heldAtLimitMessage(limit.reason, shown(limit.limit), wording.name)))
|
|
152
|
+
.join(' ');
|
|
153
|
+
}
|
|
154
|
+
/* ------------------------------- older names ------------------------------- */
|
|
155
|
+
// The shapes the fields used before the rules above were one set. They read
|
|
156
|
+
// the same rules and say the same words.
|
|
157
|
+
/** Same as {@link isMinusSign}. */
|
|
158
|
+
export const isMinusKey = isMinusSign;
|
|
159
|
+
/** Same as {@link readTypedNumber}. */
|
|
160
|
+
export const parseEntry = readTypedNumber;
|
|
161
|
+
/** A {@link SettledNumber} as an {@link EntryOutcome}. */
|
|
162
|
+
export function entryOutcome(settled) {
|
|
163
|
+
const bound = settled.limits.find((limit) => limit.reason !== 'no-minus');
|
|
164
|
+
return { value: settled.value, minusDropped: settled.limits.some((limit) => limit.reason === 'no-minus'), limit: bound ? bound.reason : null };
|
|
165
|
+
}
|
|
166
|
+
/** What typed or pasted text keeps, as an {@link EntryOutcome}; null when it is no number. */
|
|
167
|
+
export function settleEntry(text, bounds, decimal = '.') {
|
|
168
|
+
const settled = settleTypedText(text, bounds, decimal);
|
|
169
|
+
return settled && entryOutcome(settled);
|
|
170
|
+
}
|
|
171
|
+
/** An arrow-key step, as an {@link EntryOutcome} (see {@link stepTypedText}). */
|
|
172
|
+
export function stepEntry(text, change, bounds, decimal = '.') {
|
|
173
|
+
const stepped = stepTypedText(text, change, bounds, decimal);
|
|
174
|
+
return stepped && entryOutcome(stepped);
|
|
175
|
+
}
|
|
176
|
+
/** The value of an arrow-key step from text that starts with a minus sign the field refuses; null otherwise. */
|
|
177
|
+
export function stepWithoutMinus(text, change, bounds, decimal = '.') {
|
|
178
|
+
if (!refusesMinus(bounds) || !startsWithMinus(text))
|
|
179
|
+
return null;
|
|
180
|
+
return stepTypedText(text, change, bounds, decimal)?.value ?? null;
|
|
181
|
+
}
|
|
182
|
+
/** The words every number field says (the sentences above). */
|
|
183
|
+
export const NUMBER_ENTRY_WORDS = {
|
|
184
|
+
minusKey: (name) => minusIgnoredMessage(name),
|
|
185
|
+
changed(outcome, name, shown) {
|
|
186
|
+
const parts = [];
|
|
187
|
+
if (outcome.minusDropped)
|
|
188
|
+
parts.push(minusIgnoredMessage(name));
|
|
189
|
+
if (outcome.limit)
|
|
190
|
+
parts.push(heldAtLimitMessage(outcome.limit, shown, name));
|
|
191
|
+
return parts.length ? parts.join(' ') : null;
|
|
192
|
+
},
|
|
193
|
+
notANumber: (name, unit) => notANumberMessage(name, unit),
|
|
194
|
+
};
|
|
195
|
+
/** What to say for an event a number field reports; null for nothing. */
|
|
196
|
+
export function entryMessage(event, words, context) {
|
|
197
|
+
if (event.kind === 'minus-key')
|
|
198
|
+
return words.minusKey(context.name);
|
|
199
|
+
if (event.kind === 'not-a-number')
|
|
200
|
+
return words.notANumber(context.name, context.unit);
|
|
201
|
+
return words.changed(event.outcome, context.name, context.shown);
|
|
202
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A wheel turned over a focused `<input type="number">` steps its value (in
|
|
3
|
+
* browsers other than Safari), and the browser also slides the text sideways
|
|
4
|
+
* inside the box, so the number seems to jump. {@link holdNumberTextOnWheel}
|
|
5
|
+
* puts the text back where it was on the next frame. While it waits, the input
|
|
6
|
+
* carries {@link NUMBER_WHEELING_ATTR}, which hides the spin buttons (their
|
|
7
|
+
* width is what the text slides by).
|
|
8
|
+
*/
|
|
9
|
+
/** Set on a number input from a wheel turn until the frame after it. */
|
|
10
|
+
export declare const NUMBER_WHEELING_ATTR = "data-ui-wheeling";
|
|
11
|
+
/** The parts of an input the wheel fix uses (an `HTMLInputElement` has them). */
|
|
12
|
+
export interface WheelNumberInput {
|
|
13
|
+
type: string;
|
|
14
|
+
scrollLeft: number;
|
|
15
|
+
matches(selector: string): boolean;
|
|
16
|
+
hasAttribute(name: string): boolean;
|
|
17
|
+
setAttribute(name: string, value: string): void;
|
|
18
|
+
removeAttribute(name: string): void;
|
|
19
|
+
ownerDocument: {
|
|
20
|
+
defaultView: {
|
|
21
|
+
requestAnimationFrame(run: () => void): unknown;
|
|
22
|
+
navigator: {
|
|
23
|
+
vendor: string;
|
|
24
|
+
};
|
|
25
|
+
} | null;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/** Call from a number input's wheel handler: keeps its text from sliding sideways while the wheel steps the value. */
|
|
29
|
+
export declare function holdNumberTextOnWheel(input: WheelNumberInput): void;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A wheel turned over a focused `<input type="number">` steps its value (in
|
|
3
|
+
* browsers other than Safari), and the browser also slides the text sideways
|
|
4
|
+
* inside the box, so the number seems to jump. {@link holdNumberTextOnWheel}
|
|
5
|
+
* puts the text back where it was on the next frame. While it waits, the input
|
|
6
|
+
* carries {@link NUMBER_WHEELING_ATTR}, which hides the spin buttons (their
|
|
7
|
+
* width is what the text slides by).
|
|
8
|
+
*/
|
|
9
|
+
/** Set on a number input from a wheel turn until the frame after it. */
|
|
10
|
+
export const NUMBER_WHEELING_ATTR = 'data-ui-wheeling';
|
|
11
|
+
/** True in Safari and other WebKit browsers made by Apple: a wheel there does not step a number input. */
|
|
12
|
+
function wheelLeavesNumbers(view) {
|
|
13
|
+
return /Apple Computer/.test(view.navigator.vendor);
|
|
14
|
+
}
|
|
15
|
+
/** Call from a number input's wheel handler: keeps its text from sliding sideways while the wheel steps the value. */
|
|
16
|
+
export function holdNumberTextOnWheel(input) {
|
|
17
|
+
const view = input.ownerDocument.defaultView;
|
|
18
|
+
if (!view || input.type !== 'number' || wheelLeavesNumbers(view) || !input.matches(':focus') || input.hasAttribute(NUMBER_WHEELING_ATTR))
|
|
19
|
+
return;
|
|
20
|
+
const was = input.scrollLeft;
|
|
21
|
+
input.setAttribute(NUMBER_WHEELING_ATTR, '');
|
|
22
|
+
const slid = was - input.scrollLeft;
|
|
23
|
+
view.requestAnimationFrame(() => {
|
|
24
|
+
input.removeAttribute(NUMBER_WHEELING_ATTR);
|
|
25
|
+
input.scrollLeft += slid;
|
|
26
|
+
});
|
|
27
|
+
}
|