@faast-app/ui 0.0.0-stage → 0.7.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 +13 -2
- package/dist/components/Alert.d.ts +30 -0
- package/dist/components/Alert.js +19 -0
- package/dist/components/AppShell.d.ts +63 -0
- package/dist/components/AppShell.js +80 -0
- package/dist/components/Avatar.d.ts +18 -0
- package/dist/components/Avatar.js +21 -0
- package/dist/components/Badge.d.ts +24 -0
- package/dist/components/Badge.js +10 -0
- package/dist/components/Button.d.ts +71 -0
- package/dist/components/Button.js +40 -0
- package/dist/components/Card.d.ts +23 -0
- package/dist/components/Card.js +17 -0
- package/dist/components/Chip.d.ts +16 -0
- package/dist/components/Chip.js +9 -0
- package/dist/components/Choice.d.ts +38 -0
- package/dist/components/Choice.js +53 -0
- package/dist/components/Dialog.d.ts +44 -0
- package/dist/components/Dialog.js +51 -0
- package/dist/components/EmptyState.d.ts +16 -0
- package/dist/components/EmptyState.js +8 -0
- package/dist/components/FileDropArea.d.ts +62 -0
- package/dist/components/FileDropArea.js +90 -0
- package/dist/components/FormField.d.ts +35 -0
- package/dist/components/FormField.js +32 -0
- package/dist/components/HelpMenu.d.ts +46 -0
- package/dist/components/HelpMenu.js +104 -0
- package/dist/components/Icon.d.ts +24 -0
- package/dist/components/Icon.js +195 -0
- package/dist/components/Input.d.ts +37 -0
- package/dist/components/Input.js +31 -0
- package/dist/components/KpiStrip.d.ts +32 -0
- package/dist/components/KpiStrip.js +14 -0
- package/dist/components/LockScreen.d.ts +34 -0
- package/dist/components/LockScreen.js +22 -0
- package/dist/components/Logo.d.ts +27 -0
- package/dist/components/Logo.js +12 -0
- package/dist/components/Navigation.d.ts +37 -0
- package/dist/components/Navigation.js +29 -0
- package/dist/components/OptionList.d.ts +27 -0
- package/dist/components/OptionList.js +18 -0
- package/dist/components/OtpInput.d.ts +31 -0
- package/dist/components/OtpInput.js +102 -0
- package/dist/components/Pagination.d.ts +31 -0
- package/dist/components/Pagination.js +41 -0
- package/dist/components/PasswordField.d.ts +32 -0
- package/dist/components/PasswordField.js +29 -0
- package/dist/components/SegmentedControl.d.ts +35 -0
- package/dist/components/SegmentedControl.js +52 -0
- package/dist/components/Select.d.ts +15 -0
- package/dist/components/Select.js +10 -0
- package/dist/components/SessionTimeoutDialog.d.ts +39 -0
- package/dist/components/SessionTimeoutDialog.js +62 -0
- package/dist/components/Spinner.d.ts +25 -0
- package/dist/components/Spinner.js +15 -0
- package/dist/components/StatTiles.d.ts +35 -0
- package/dist/components/StatTiles.js +12 -0
- package/dist/components/Stepper.d.ts +26 -0
- package/dist/components/Stepper.js +26 -0
- package/dist/components/StickyActionBar.d.ts +28 -0
- package/dist/components/StickyActionBar.js +12 -0
- package/dist/components/Table.d.ts +69 -0
- package/dist/components/Table.js +73 -0
- package/dist/components/TenantMark.d.ts +27 -0
- package/dist/components/TenantMark.js +15 -0
- package/dist/components/Timeline.d.ts +31 -0
- package/dist/components/Timeline.js +18 -0
- package/dist/components/Toast.d.ts +43 -0
- package/dist/components/Toast.js +46 -0
- package/dist/components/Toggletip.d.ts +26 -0
- package/dist/components/Toggletip.js +47 -0
- package/dist/components/Tooltip.d.ts +24 -0
- package/dist/components/Tooltip.js +73 -0
- package/dist/components/Tour.d.ts +66 -0
- package/dist/components/Tour.js +161 -0
- package/dist/components/TourOffer.d.ts +22 -0
- package/dist/components/TourOffer.js +33 -0
- package/dist/components/WelcomeDialog.d.ts +45 -0
- package/dist/components/WelcomeDialog.js +38 -0
- package/dist/data-table/components/ClientDataTable.d.ts +22 -0
- package/dist/data-table/components/ClientDataTable.js +29 -0
- package/dist/data-table/components/DataTableFooter.d.ts +11 -0
- package/dist/data-table/components/DataTableFooter.js +16 -0
- package/dist/data-table/components/DataTableView.d.ts +9 -0
- package/dist/data-table/components/DataTableView.js +95 -0
- package/dist/data-table/components/ServerDataTable.d.ts +16 -0
- package/dist/data-table/components/ServerDataTable.js +17 -0
- package/dist/data-table/core/columnVisibility.d.ts +15 -0
- package/dist/data-table/core/columnVisibility.js +34 -0
- package/dist/data-table/core/constants.d.ts +20 -0
- package/dist/data-table/core/constants.js +21 -0
- package/dist/data-table/core/filters/registry.d.ts +33 -0
- package/dist/data-table/core/filters/registry.js +393 -0
- package/dist/data-table/core/filters/types.d.ts +88 -0
- package/dist/data-table/core/filters/types.js +1 -0
- package/dist/data-table/core/group.d.ts +15 -0
- package/dist/data-table/core/group.js +73 -0
- package/dist/data-table/core/paginate.d.ts +13 -0
- package/dist/data-table/core/paginate.js +27 -0
- package/dist/data-table/core/query.d.ts +65 -0
- package/dist/data-table/core/query.js +138 -0
- package/dist/data-table/core/search.d.ts +10 -0
- package/dist/data-table/core/search.js +35 -0
- package/dist/data-table/core/selection.d.ts +24 -0
- package/dist/data-table/core/selection.js +59 -0
- package/dist/data-table/core/sort.d.ts +14 -0
- package/dist/data-table/core/sort.js +41 -0
- package/dist/data-table/core/text.d.ts +5 -0
- package/dist/data-table/core/text.js +27 -0
- package/dist/data-table/core/types.d.ts +40 -0
- package/dist/data-table/core/types.js +1 -0
- package/dist/data-table/core/url.d.ts +43 -0
- package/dist/data-table/core/url.js +194 -0
- package/dist/data-table/hooks/common.d.ts +14 -0
- package/dist/data-table/hooks/common.js +30 -0
- package/dist/data-table/hooks/small-hooks.d.ts +12 -0
- package/dist/data-table/hooks/small-hooks.js +111 -0
- package/dist/data-table/hooks/useClientDataTable.d.ts +35 -0
- package/dist/data-table/hooks/useClientDataTable.js +311 -0
- package/dist/data-table/hooks/useServerDataTable.d.ts +38 -0
- package/dist/data-table/hooks/useServerDataTable.js +209 -0
- package/dist/data-table/hooks/useServerRows.d.ts +27 -0
- package/dist/data-table/hooks/useServerRows.js +82 -0
- package/dist/data-table/hooks/useTableState.d.ts +39 -0
- package/dist/data-table/hooks/useTableState.js +219 -0
- package/dist/data-table/index.d.ts +42 -0
- package/dist/data-table/index.js +22 -0
- package/dist/data-table/labels.d.ts +77 -0
- package/dist/data-table/labels.js +77 -0
- package/dist/data-table/toolbar/ActiveFilters.d.ts +10 -0
- package/dist/data-table/toolbar/ActiveFilters.js +53 -0
- package/dist/data-table/toolbar/BulkActionBar.d.ts +15 -0
- package/dist/data-table/toolbar/BulkActionBar.js +22 -0
- package/dist/data-table/toolbar/ColumnVisibilityDialog.d.ts +7 -0
- package/dist/data-table/toolbar/ColumnVisibilityDialog.js +15 -0
- package/dist/data-table/toolbar/DataTableToolbar.d.ts +19 -0
- package/dist/data-table/toolbar/DataTableToolbar.js +12 -0
- package/dist/data-table/toolbar/FilterFields.d.ts +11 -0
- package/dist/data-table/toolbar/FilterFields.js +58 -0
- package/dist/data-table/toolbar/FiltersControl.d.ts +34 -0
- package/dist/data-table/toolbar/FiltersControl.js +70 -0
- package/dist/data-table/toolbar/PageSizeSelect.d.ts +7 -0
- package/dist/data-table/toolbar/PageSizeSelect.js +12 -0
- package/dist/data-table/toolbar/QuickFilters.d.ts +7 -0
- package/dist/data-table/toolbar/QuickFilters.js +9 -0
- package/dist/data-table/toolbar/ResultsCount.d.ts +7 -0
- package/dist/data-table/toolbar/ResultsCount.js +9 -0
- package/dist/data-table/toolbar/SearchField.d.ts +36 -0
- package/dist/data-table/toolbar/SearchField.js +64 -0
- package/dist/data-table/toolbar/SortSelect.d.ts +10 -0
- package/dist/data-table/toolbar/SortSelect.js +30 -0
- package/dist/data-table/types.d.ts +334 -0
- package/dist/data-table/types.js +1 -0
- package/dist/index.d.ts +91 -0
- package/dist/index.js +49 -0
- package/dist/styles.css +4668 -0
- package/dist/theme/ThemeProvider.d.ts +33 -0
- package/dist/theme/ThemeProvider.js +77 -0
- package/dist/theme/ThemeToggle.d.ts +16 -0
- package/dist/theme/ThemeToggle.js +27 -0
- package/dist/utils/cx.d.ts +2 -0
- package/dist/utils/cx.js +4 -0
- package/dist/utils/devWarn.d.ts +2 -0
- package/dist/utils/devWarn.js +10 -0
- package/dist/utils/tourGeometry.d.ts +61 -0
- package/dist/utils/tourGeometry.js +135 -0
- package/dist/utils/useFloatingPosition.d.ts +9 -0
- package/dist/utils/useFloatingPosition.js +45 -0
- package/dist/utils/useModalDialog.d.ts +7 -0
- package/dist/utils/useModalDialog.js +49 -0
- package/dist/utils/useTourAnchorRect.d.ts +6 -0
- package/dist/utils/useTourAnchorRect.js +40 -0
- package/package.json +50 -4
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type ThemeMode } from '@faast-app/themes';
|
|
2
|
+
import { type ReactNode } from 'react';
|
|
3
|
+
export type ResolvedTheme = 'light' | 'dark';
|
|
4
|
+
export interface ThemeContextValue {
|
|
5
|
+
/** The user's choice: `system` follows the OS preference. */
|
|
6
|
+
mode: ThemeMode;
|
|
7
|
+
/** The theme actually shown. */
|
|
8
|
+
resolvedTheme: ResolvedTheme;
|
|
9
|
+
setMode: (mode: ThemeMode) => void;
|
|
10
|
+
}
|
|
11
|
+
export interface ThemeProviderProps {
|
|
12
|
+
children?: ReactNode;
|
|
13
|
+
/** Mode used when nothing valid is stored. Default `system`. */
|
|
14
|
+
defaultMode?: ThemeMode;
|
|
15
|
+
/** localStorage key. Must match the init snippet; default `faast-theme`. */
|
|
16
|
+
storageKey?: string;
|
|
17
|
+
/** Element that receives `data-theme`. Default `document.documentElement`. */
|
|
18
|
+
target?: HTMLElement | null;
|
|
19
|
+
/**
|
|
20
|
+
* Controlled mode (e.g. synced with a user profile or a Storybook toolbar). When set, the
|
|
21
|
+
* provider does not read or write localStorage; handle `onModeChange` to update it.
|
|
22
|
+
*/
|
|
23
|
+
mode?: ThemeMode;
|
|
24
|
+
/** Called with the requested mode (controlled and uncontrolled). */
|
|
25
|
+
onModeChange?: (mode: ThemeMode) => void;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Holds the theme preference (`system` | `light` | `dark`), persists it in localStorage and
|
|
29
|
+
* mirrors it on `data-theme`. `system` removes the attribute so the CSS media query decides.
|
|
30
|
+
*/
|
|
31
|
+
export declare function ThemeProvider({ children, defaultMode, storageKey, target, mode: controlledMode, onModeChange, }: ThemeProviderProps): import("react").JSX.Element;
|
|
32
|
+
/** Reads the theme context. Must be used inside `ThemeProvider`. */
|
|
33
|
+
export declare function useTheme(): ThemeContextValue;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { THEME_ATTRIBUTE, THEME_STORAGE_KEY } from '@faast-app/themes';
|
|
3
|
+
import { createContext, useCallback, useContext, useEffect, useMemo, useState, } from 'react';
|
|
4
|
+
const ThemeContext = createContext(null);
|
|
5
|
+
const DARK_QUERY = '(prefers-color-scheme: dark)';
|
|
6
|
+
function isMode(value) {
|
|
7
|
+
return value === 'system' || value === 'light' || value === 'dark';
|
|
8
|
+
}
|
|
9
|
+
function readStored(key) {
|
|
10
|
+
try {
|
|
11
|
+
const value = typeof window === 'undefined' ? null : window.localStorage.getItem(key);
|
|
12
|
+
return isMode(value) ? value : null;
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function writeStored(key, mode) {
|
|
19
|
+
try {
|
|
20
|
+
window.localStorage.setItem(key, mode);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
// Storage blocked (private mode, policy): the choice lasts for this session only.
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function systemPrefersDark() {
|
|
27
|
+
return typeof window !== 'undefined' && typeof window.matchMedia === 'function'
|
|
28
|
+
? window.matchMedia(DARK_QUERY).matches
|
|
29
|
+
: false;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Holds the theme preference (`system` | `light` | `dark`), persists it in localStorage and
|
|
33
|
+
* mirrors it on `data-theme`. `system` removes the attribute so the CSS media query decides.
|
|
34
|
+
*/
|
|
35
|
+
export function ThemeProvider({ children, defaultMode = 'system', storageKey = THEME_STORAGE_KEY, target, mode: controlledMode, onModeChange, }) {
|
|
36
|
+
const controlled = controlledMode !== undefined;
|
|
37
|
+
const [internalMode, setInternalMode] = useState(() => controlled ? defaultMode : (readStored(storageKey) ?? defaultMode));
|
|
38
|
+
const mode = controlledMode ?? internalMode;
|
|
39
|
+
const [systemDark, setSystemDark] = useState(systemPrefersDark);
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
if (typeof window === 'undefined' || typeof window.matchMedia !== 'function')
|
|
42
|
+
return;
|
|
43
|
+
const query = window.matchMedia(DARK_QUERY);
|
|
44
|
+
const onChange = (event) => setSystemDark(event.matches);
|
|
45
|
+
query.addEventListener('change', onChange);
|
|
46
|
+
return () => query.removeEventListener('change', onChange);
|
|
47
|
+
}, []);
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
const element = target ?? (typeof document === 'undefined' ? null : document.documentElement);
|
|
50
|
+
if (!element)
|
|
51
|
+
return;
|
|
52
|
+
if (mode === 'system')
|
|
53
|
+
element.removeAttribute(THEME_ATTRIBUTE);
|
|
54
|
+
else
|
|
55
|
+
element.setAttribute(THEME_ATTRIBUTE, mode);
|
|
56
|
+
}, [mode, target]);
|
|
57
|
+
const setMode = useCallback((next) => {
|
|
58
|
+
if (!controlled) {
|
|
59
|
+
setInternalMode(next);
|
|
60
|
+
writeStored(storageKey, next);
|
|
61
|
+
}
|
|
62
|
+
onModeChange?.(next);
|
|
63
|
+
}, [controlled, storageKey, onModeChange]);
|
|
64
|
+
const value = useMemo(() => ({
|
|
65
|
+
mode,
|
|
66
|
+
resolvedTheme: mode === 'system' ? (systemDark ? 'dark' : 'light') : mode,
|
|
67
|
+
setMode,
|
|
68
|
+
}), [mode, systemDark, setMode]);
|
|
69
|
+
return _jsx(ThemeContext.Provider, { value: value, children: children });
|
|
70
|
+
}
|
|
71
|
+
/** Reads the theme context. Must be used inside `ThemeProvider`. */
|
|
72
|
+
export function useTheme() {
|
|
73
|
+
const context = useContext(ThemeContext);
|
|
74
|
+
if (!context)
|
|
75
|
+
throw new Error('useTheme must be used inside <ThemeProvider>.');
|
|
76
|
+
return context;
|
|
77
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface ThemeToggleLabels {
|
|
2
|
+
/** Shown when the next click switches to light. */
|
|
3
|
+
toLight: string;
|
|
4
|
+
/** Shown when the next click switches to dark. */
|
|
5
|
+
toDark: string;
|
|
6
|
+
/** Shown when the next click switches to the system setting. */
|
|
7
|
+
toSystem: string;
|
|
8
|
+
}
|
|
9
|
+
export interface ThemeToggleProps {
|
|
10
|
+
/** Cycle light, dark and system instead of toggling light/dark. Default `false`. */
|
|
11
|
+
includeSystem?: boolean;
|
|
12
|
+
labels?: Partial<ThemeToggleLabels>;
|
|
13
|
+
className?: string;
|
|
14
|
+
}
|
|
15
|
+
/** Icon button that switches the theme. Its label names the action it performs. */
|
|
16
|
+
export declare function ThemeToggle({ includeSystem, labels, className }: ThemeToggleProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { IconButton } from '../components/Button.js';
|
|
3
|
+
import { Icon } from '../components/Icon.js';
|
|
4
|
+
import { useTheme } from './ThemeProvider.js';
|
|
5
|
+
const DEFAULT_LABELS = {
|
|
6
|
+
toLight: 'Activar Tema Claro',
|
|
7
|
+
toDark: 'Activar Tema Oscuro',
|
|
8
|
+
toSystem: 'Usar Tema del Sistema',
|
|
9
|
+
};
|
|
10
|
+
function nextMode(mode, resolved, includeSystem) {
|
|
11
|
+
if (!includeSystem)
|
|
12
|
+
return resolved === 'dark' ? 'light' : 'dark';
|
|
13
|
+
if (mode === 'system')
|
|
14
|
+
return resolved === 'dark' ? 'light' : 'dark';
|
|
15
|
+
if (mode === 'light')
|
|
16
|
+
return 'dark';
|
|
17
|
+
return 'system';
|
|
18
|
+
}
|
|
19
|
+
/** Icon button that switches the theme. Its label names the action it performs. */
|
|
20
|
+
export function ThemeToggle({ includeSystem = false, labels, className }) {
|
|
21
|
+
const { mode, resolvedTheme, setMode } = useTheme();
|
|
22
|
+
const text = { ...DEFAULT_LABELS, ...labels };
|
|
23
|
+
const next = nextMode(mode, resolvedTheme, includeSystem);
|
|
24
|
+
const label = next === 'light' ? text.toLight : next === 'dark' ? text.toDark : text.toSystem;
|
|
25
|
+
const icon = (_jsx(Icon, { name: next === 'light' ? 'sun' : next === 'dark' ? 'moon' : 'monitor', size: 16 }));
|
|
26
|
+
return (_jsx(IconButton, { label: label, icon: icon, className: className, "data-theme-mode": mode, onClick: () => setMode(next) }));
|
|
27
|
+
}
|
package/dist/utils/cx.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
const warned = new Set();
|
|
2
|
+
/** One-time development warning; silent in production builds. */
|
|
3
|
+
export function devWarn(message) {
|
|
4
|
+
if (typeof process !== 'undefined' && process.env?.NODE_ENV === 'production')
|
|
5
|
+
return;
|
|
6
|
+
if (warned.has(message))
|
|
7
|
+
return;
|
|
8
|
+
warned.add(message);
|
|
9
|
+
console.warn(`[@faast-app/ui] ${message}`);
|
|
10
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/** Pure geometry for the guided tour: visibility, rect union, popover placement and reveal. */
|
|
2
|
+
export type TourSide = 'top' | 'bottom' | 'left' | 'right';
|
|
3
|
+
export type TourPlacedSide = TourSide | 'none';
|
|
4
|
+
export interface TourRect {
|
|
5
|
+
readonly left: number;
|
|
6
|
+
readonly top: number;
|
|
7
|
+
readonly right: number;
|
|
8
|
+
readonly bottom: number;
|
|
9
|
+
readonly width: number;
|
|
10
|
+
readonly height: number;
|
|
11
|
+
}
|
|
12
|
+
export interface PlaceTourPopoverInput {
|
|
13
|
+
/** The highlighted rect (without the spotlight padding). */
|
|
14
|
+
anchor: TourRect;
|
|
15
|
+
/** Measured size of the popover. */
|
|
16
|
+
size: {
|
|
17
|
+
readonly width: number;
|
|
18
|
+
readonly height: number;
|
|
19
|
+
};
|
|
20
|
+
viewport: {
|
|
21
|
+
readonly width: number;
|
|
22
|
+
readonly height: number;
|
|
23
|
+
};
|
|
24
|
+
preferred: TourSide;
|
|
25
|
+
}
|
|
26
|
+
export interface TourPopoverPlacement {
|
|
27
|
+
side: TourPlacedSide;
|
|
28
|
+
x: number;
|
|
29
|
+
y: number;
|
|
30
|
+
/** Arrow position along the top/bottom edge (px from the popover's left). */
|
|
31
|
+
arrowX: number;
|
|
32
|
+
/** Arrow position along the left/right edge (px from the popover's top). */
|
|
33
|
+
arrowY: number;
|
|
34
|
+
}
|
|
35
|
+
/** Space between the spotlight and the popover. */
|
|
36
|
+
export declare const TOUR_GAP = 14;
|
|
37
|
+
/** Spotlight padding around the target. */
|
|
38
|
+
export declare const TOUR_PADDING = 6;
|
|
39
|
+
/** Minimum space kept between the popover and the viewport edge. */
|
|
40
|
+
export declare const TOUR_VIEWPORT_MARGIN = 8;
|
|
41
|
+
/**
|
|
42
|
+
* Whether a tour target can be highlighted: connected, no `[hidden]` ancestor, not
|
|
43
|
+
* `display: none` / `visibility: hidden`, and a non-empty box.
|
|
44
|
+
*/
|
|
45
|
+
export declare function isTourTargetVisible(el: Element): boolean;
|
|
46
|
+
/** The smallest rect containing every element's box, or `null` for an empty list. */
|
|
47
|
+
export declare function unionRect(els: readonly Element[]): DOMRectReadOnly | null;
|
|
48
|
+
/**
|
|
49
|
+
* Chooses the side of the target for the popover: the preferred one, its opposite, then bottom,
|
|
50
|
+
* top, right, left; the first that fits wins. When none fits the popover sits at the viewport's
|
|
51
|
+
* bottom-end corner without an arrow (`side: 'none'`).
|
|
52
|
+
*/
|
|
53
|
+
export declare function placeTourPopover({ anchor, size, viewport, preferred, }: PlaceTourPopoverInput): TourPopoverPlacement;
|
|
54
|
+
/**
|
|
55
|
+
* Scrolls a target into view before it is highlighted: centred, or at the start on phones and
|
|
56
|
+
* for tall targets. Targets inside a fixed or sticky ancestor never scroll with the page, so
|
|
57
|
+
* they are left alone.
|
|
58
|
+
*/
|
|
59
|
+
export declare function revealTourTarget(el: Element, { phone }: {
|
|
60
|
+
phone: boolean;
|
|
61
|
+
}): void;
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/** Pure geometry for the guided tour: visibility, rect union, popover placement and reveal. */
|
|
2
|
+
/** Space between the spotlight and the popover. */
|
|
3
|
+
export const TOUR_GAP = 14;
|
|
4
|
+
/** Spotlight padding around the target. */
|
|
5
|
+
export const TOUR_PADDING = 6;
|
|
6
|
+
/** Minimum space kept between the popover and the viewport edge. */
|
|
7
|
+
export const TOUR_VIEWPORT_MARGIN = 8;
|
|
8
|
+
const ARROW_INSET = 22;
|
|
9
|
+
const OPPOSITE = {
|
|
10
|
+
top: 'bottom',
|
|
11
|
+
bottom: 'top',
|
|
12
|
+
left: 'right',
|
|
13
|
+
right: 'left',
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Whether a tour target can be highlighted: connected, no `[hidden]` ancestor, not
|
|
17
|
+
* `display: none` / `visibility: hidden`, and a non-empty box.
|
|
18
|
+
*/
|
|
19
|
+
export function isTourTargetVisible(el) {
|
|
20
|
+
if (!el.isConnected)
|
|
21
|
+
return false;
|
|
22
|
+
if (el.closest('[hidden]'))
|
|
23
|
+
return false;
|
|
24
|
+
const style = getComputedStyle(el);
|
|
25
|
+
if (style.display === 'none' || style.visibility === 'hidden')
|
|
26
|
+
return false;
|
|
27
|
+
const rect = el.getBoundingClientRect();
|
|
28
|
+
return rect.width > 0 && rect.height > 0;
|
|
29
|
+
}
|
|
30
|
+
/** The smallest rect containing every element's box, or `null` for an empty list. */
|
|
31
|
+
export function unionRect(els) {
|
|
32
|
+
let left = Infinity;
|
|
33
|
+
let top = Infinity;
|
|
34
|
+
let right = -Infinity;
|
|
35
|
+
let bottom = -Infinity;
|
|
36
|
+
for (const el of els) {
|
|
37
|
+
const r = el.getBoundingClientRect();
|
|
38
|
+
left = Math.min(left, r.left);
|
|
39
|
+
top = Math.min(top, r.top);
|
|
40
|
+
right = Math.max(right, r.right);
|
|
41
|
+
bottom = Math.max(bottom, r.bottom);
|
|
42
|
+
}
|
|
43
|
+
if (!Number.isFinite(left))
|
|
44
|
+
return null;
|
|
45
|
+
const width = right - left;
|
|
46
|
+
const height = bottom - top;
|
|
47
|
+
return {
|
|
48
|
+
x: left,
|
|
49
|
+
y: top,
|
|
50
|
+
left,
|
|
51
|
+
top,
|
|
52
|
+
right,
|
|
53
|
+
bottom,
|
|
54
|
+
width,
|
|
55
|
+
height,
|
|
56
|
+
toJSON: () => ({ left, top, width, height }),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Chooses the side of the target for the popover: the preferred one, its opposite, then bottom,
|
|
61
|
+
* top, right, left; the first that fits wins. When none fits the popover sits at the viewport's
|
|
62
|
+
* bottom-end corner without an arrow (`side: 'none'`).
|
|
63
|
+
*/
|
|
64
|
+
export function placeTourPopover({ anchor, size, viewport, preferred, }) {
|
|
65
|
+
const { width: w, height: h } = size;
|
|
66
|
+
const { width: vw, height: vh } = viewport;
|
|
67
|
+
const pad = TOUR_PADDING;
|
|
68
|
+
const gap = TOUR_GAP;
|
|
69
|
+
const m = TOUR_VIEWPORT_MARGIN;
|
|
70
|
+
const clampX = (x) => Math.max(m, Math.min(x, vw - w - m));
|
|
71
|
+
const clampY = (y) => Math.max(m, Math.min(y, vh - h - m));
|
|
72
|
+
const centreX = anchor.left + anchor.width / 2;
|
|
73
|
+
const centreY = anchor.top + anchor.height / 2;
|
|
74
|
+
const spots = {
|
|
75
|
+
bottom: () => ({
|
|
76
|
+
ok: anchor.bottom + pad + gap + h <= vh - m,
|
|
77
|
+
x: clampX(centreX - w / 2),
|
|
78
|
+
y: anchor.bottom + pad + gap,
|
|
79
|
+
}),
|
|
80
|
+
top: () => ({
|
|
81
|
+
ok: anchor.top - pad - gap - h >= m,
|
|
82
|
+
x: clampX(centreX - w / 2),
|
|
83
|
+
y: anchor.top - pad - gap - h,
|
|
84
|
+
}),
|
|
85
|
+
right: () => ({
|
|
86
|
+
ok: anchor.right + pad + gap + w <= vw - m,
|
|
87
|
+
x: anchor.right + pad + gap,
|
|
88
|
+
y: clampY(centreY - h / 2),
|
|
89
|
+
}),
|
|
90
|
+
left: () => ({
|
|
91
|
+
ok: anchor.left - pad - gap - w >= m,
|
|
92
|
+
x: anchor.left - pad - gap - w,
|
|
93
|
+
y: clampY(centreY - h / 2),
|
|
94
|
+
}),
|
|
95
|
+
};
|
|
96
|
+
const order = [preferred, OPPOSITE[preferred], 'bottom', 'top', 'right', 'left'];
|
|
97
|
+
let side = 'none';
|
|
98
|
+
let pos = { x: clampX(vw - w - 24), y: clampY(vh - h - 24) };
|
|
99
|
+
for (const candidate of order) {
|
|
100
|
+
const spot = spots[candidate]();
|
|
101
|
+
if (spot.ok) {
|
|
102
|
+
side = candidate;
|
|
103
|
+
pos = { x: spot.x, y: spot.y };
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const clampArrow = (value, max) => Math.max(ARROW_INSET, Math.min(max - ARROW_INSET, value));
|
|
108
|
+
return {
|
|
109
|
+
side,
|
|
110
|
+
x: pos.x,
|
|
111
|
+
y: pos.y,
|
|
112
|
+
arrowX: clampArrow(centreX - pos.x, w),
|
|
113
|
+
arrowY: clampArrow(centreY - pos.y, h),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
function hasFixedOrStickyAncestor(el) {
|
|
117
|
+
for (let node = el; node; node = node.parentElement) {
|
|
118
|
+
const position = getComputedStyle(node).position;
|
|
119
|
+
if (position === 'fixed' || position === 'sticky')
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Scrolls a target into view before it is highlighted: centred, or at the start on phones and
|
|
126
|
+
* for tall targets. Targets inside a fixed or sticky ancestor never scroll with the page, so
|
|
127
|
+
* they are left alone.
|
|
128
|
+
*/
|
|
129
|
+
export function revealTourTarget(el, { phone }) {
|
|
130
|
+
if (hasFixedOrStickyAncestor(el))
|
|
131
|
+
return;
|
|
132
|
+
const rect = el.getBoundingClientRect();
|
|
133
|
+
const tall = rect.height > window.innerHeight * 0.6;
|
|
134
|
+
el.scrollIntoView({ block: phone || tall ? 'start' : 'center', behavior: 'auto' });
|
|
135
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type RefObject } from 'react';
|
|
2
|
+
export type FloatingPlacement = 'top' | 'bottom';
|
|
3
|
+
/**
|
|
4
|
+
* Positions a fixed element next to an anchor while `open`: on the preferred side (flipped
|
|
5
|
+
* when it does not fit), centred on the anchor and shifted to stay inside the viewport.
|
|
6
|
+
* Fixed positioning keeps it from being clipped by scrolling or overflow ancestors, so it can
|
|
7
|
+
* live next to its trigger in the DOM (which also keeps it inside an open modal dialog).
|
|
8
|
+
*/
|
|
9
|
+
export declare function useFloatingPosition(anchor: RefObject<HTMLElement | null>, floating: RefObject<HTMLElement | null>, open: boolean, placement: FloatingPlacement): void;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { useLayoutEffect } from 'react';
|
|
2
|
+
const GAP = 6;
|
|
3
|
+
const MARGIN = 8;
|
|
4
|
+
function place(anchorEl, floatingEl, placement) {
|
|
5
|
+
const a = anchorEl.getBoundingClientRect();
|
|
6
|
+
const f = floatingEl.getBoundingClientRect();
|
|
7
|
+
const viewportWidth = document.documentElement.clientWidth;
|
|
8
|
+
const viewportHeight = document.documentElement.clientHeight;
|
|
9
|
+
const roomAbove = a.top - MARGIN;
|
|
10
|
+
const roomBelow = viewportHeight - a.bottom - MARGIN;
|
|
11
|
+
const need = f.height + GAP;
|
|
12
|
+
const fitsPreferred = placement === 'top' ? roomAbove >= need : roomBelow >= need;
|
|
13
|
+
const preferredHasMore = placement === 'top' ? roomAbove >= roomBelow : roomBelow >= roomAbove;
|
|
14
|
+
const opposite = placement === 'top' ? 'bottom' : 'top';
|
|
15
|
+
const side = fitsPreferred || preferredHasMore ? placement : opposite;
|
|
16
|
+
const top = side === 'top' ? a.top - f.height - GAP : a.bottom + GAP;
|
|
17
|
+
const centred = a.left + a.width / 2 - f.width / 2;
|
|
18
|
+
const left = Math.max(MARGIN, Math.min(centred, viewportWidth - f.width - MARGIN));
|
|
19
|
+
floatingEl.style.top = `${String(Math.max(MARGIN, top))}px`;
|
|
20
|
+
floatingEl.style.left = `${String(left)}px`;
|
|
21
|
+
floatingEl.dataset.side = side;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Positions a fixed element next to an anchor while `open`: on the preferred side (flipped
|
|
25
|
+
* when it does not fit), centred on the anchor and shifted to stay inside the viewport.
|
|
26
|
+
* Fixed positioning keeps it from being clipped by scrolling or overflow ancestors, so it can
|
|
27
|
+
* live next to its trigger in the DOM (which also keeps it inside an open modal dialog).
|
|
28
|
+
*/
|
|
29
|
+
export function useFloatingPosition(anchor, floating, open, placement) {
|
|
30
|
+
useLayoutEffect(() => {
|
|
31
|
+
if (!open)
|
|
32
|
+
return;
|
|
33
|
+
const update = () => {
|
|
34
|
+
if (anchor.current && floating.current)
|
|
35
|
+
place(anchor.current, floating.current, placement);
|
|
36
|
+
};
|
|
37
|
+
update();
|
|
38
|
+
window.addEventListener('resize', update);
|
|
39
|
+
window.addEventListener('scroll', update, { capture: true, passive: true });
|
|
40
|
+
return () => {
|
|
41
|
+
window.removeEventListener('resize', update);
|
|
42
|
+
window.removeEventListener('scroll', update, { capture: true });
|
|
43
|
+
};
|
|
44
|
+
}, [anchor, floating, open, placement]);
|
|
45
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type RefObject } from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* Drives a native `<dialog>` from a controlled `open` prop: `showModal()` (focus trap,
|
|
4
|
+
* inert page, top layer) on open, `close()` on close or unmount, and focus returns to the
|
|
5
|
+
* element that was focused before opening. `onOpened` runs after `showModal()`.
|
|
6
|
+
*/
|
|
7
|
+
export declare function useModalDialog(ref: RefObject<HTMLDialogElement | null>, open: boolean, onOpened?: () => void): void;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { useEffect, useRef } from 'react';
|
|
2
|
+
function openModal(dialog) {
|
|
3
|
+
if (typeof dialog.showModal === 'function')
|
|
4
|
+
dialog.showModal();
|
|
5
|
+
else
|
|
6
|
+
dialog.setAttribute('open', '');
|
|
7
|
+
}
|
|
8
|
+
function closeModal(dialog) {
|
|
9
|
+
if (typeof dialog.close === 'function')
|
|
10
|
+
dialog.close();
|
|
11
|
+
else
|
|
12
|
+
dialog.removeAttribute('open');
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Drives a native `<dialog>` from a controlled `open` prop: `showModal()` (focus trap,
|
|
16
|
+
* inert page, top layer) on open, `close()` on close or unmount, and focus returns to the
|
|
17
|
+
* element that was focused before opening. `onOpened` runs after `showModal()`.
|
|
18
|
+
*/
|
|
19
|
+
export function useModalDialog(ref, open, onOpened) {
|
|
20
|
+
const returnFocus = useRef(null);
|
|
21
|
+
const onOpenedRef = useRef(onOpened);
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
onOpenedRef.current = onOpened;
|
|
24
|
+
});
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
const dialog = ref.current;
|
|
27
|
+
if (!dialog)
|
|
28
|
+
return;
|
|
29
|
+
if (open && !dialog.open) {
|
|
30
|
+
returnFocus.current =
|
|
31
|
+
document.activeElement instanceof HTMLElement ? document.activeElement : null;
|
|
32
|
+
openModal(dialog);
|
|
33
|
+
onOpenedRef.current?.();
|
|
34
|
+
}
|
|
35
|
+
else if (!open && dialog.open) {
|
|
36
|
+
closeModal(dialog);
|
|
37
|
+
returnFocus.current?.focus();
|
|
38
|
+
returnFocus.current = null;
|
|
39
|
+
}
|
|
40
|
+
}, [open, ref]);
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
const dialog = ref.current;
|
|
43
|
+
return () => {
|
|
44
|
+
if (dialog?.open)
|
|
45
|
+
closeModal(dialog);
|
|
46
|
+
returnFocus.current?.focus();
|
|
47
|
+
};
|
|
48
|
+
}, [ref]);
|
|
49
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The union rect of the highlighted elements, kept current while they exist: it is re-measured
|
|
3
|
+
* on resize, on any scroll (capture), when an element resizes and on the next animation frame
|
|
4
|
+
* (so a target revealed by scrolling or by a layout change is picked up). `null` without elements.
|
|
5
|
+
*/
|
|
6
|
+
export declare function useTourAnchorRect(elements: readonly Element[] | null): DOMRectReadOnly | null;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
|
+
import { unionRect } from './tourGeometry.js';
|
|
3
|
+
function sameRect(a, b) {
|
|
4
|
+
if (a === b)
|
|
5
|
+
return true;
|
|
6
|
+
if (!a || !b)
|
|
7
|
+
return false;
|
|
8
|
+
return a.left === b.left && a.top === b.top && a.width === b.width && a.height === b.height;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The union rect of the highlighted elements, kept current while they exist: it is re-measured
|
|
12
|
+
* on resize, on any scroll (capture), when an element resizes and on the next animation frame
|
|
13
|
+
* (so a target revealed by scrolling or by a layout change is picked up). `null` without elements.
|
|
14
|
+
*/
|
|
15
|
+
export function useTourAnchorRect(elements) {
|
|
16
|
+
const [rect, setRect] = useState(null);
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
if (!elements || elements.length === 0)
|
|
19
|
+
return;
|
|
20
|
+
const measure = () => {
|
|
21
|
+
const next = unionRect(elements);
|
|
22
|
+
setRect((previous) => (sameRect(previous, next) ? previous : next));
|
|
23
|
+
};
|
|
24
|
+
measure();
|
|
25
|
+
const frame = requestAnimationFrame(measure);
|
|
26
|
+
window.addEventListener('resize', measure);
|
|
27
|
+
window.addEventListener('scroll', measure, { capture: true, passive: true });
|
|
28
|
+
const observer = typeof ResizeObserver === 'function' ? new ResizeObserver(measure) : null;
|
|
29
|
+
if (observer)
|
|
30
|
+
for (const el of elements)
|
|
31
|
+
observer.observe(el);
|
|
32
|
+
return () => {
|
|
33
|
+
cancelAnimationFrame(frame);
|
|
34
|
+
window.removeEventListener('resize', measure);
|
|
35
|
+
window.removeEventListener('scroll', measure, { capture: true });
|
|
36
|
+
observer?.disconnect();
|
|
37
|
+
};
|
|
38
|
+
}, [elements]);
|
|
39
|
+
return elements && elements.length > 0 ? rect : null;
|
|
40
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faast-app/ui",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.7.0",
|
|
4
|
+
"description": "FaaST accessible React UI components built on @faast-app/tokens and @faast-app/themes.",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": [
|
|
8
|
+
"*.css"
|
|
9
|
+
],
|
|
10
|
+
"main": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"./styles.css": "./dist/styles.css",
|
|
18
|
+
"./package.json": "./package.json"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@testing-library/dom": "10",
|
|
28
|
+
"@testing-library/jest-dom": "6.10.0",
|
|
29
|
+
"@testing-library/react": "16.3.3",
|
|
30
|
+
"@testing-library/user-event": "14.6.8",
|
|
31
|
+
"@types/react": "19.3.0",
|
|
32
|
+
"@types/react-dom": "19.3.0",
|
|
33
|
+
"jsdom": "26.1.0",
|
|
34
|
+
"react": "19.3.1",
|
|
35
|
+
"react-dom": "19.3.1"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@faast-app/themes": "^0.7.0",
|
|
39
|
+
"@faast-app/tokens": "^0.7.0",
|
|
40
|
+
"lucide-react": "1.58.0"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"react": "^18.2.0 || ^19.0.0",
|
|
44
|
+
"react-dom": "^18.2.0 || ^19.0.0"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"clean": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\"",
|
|
48
|
+
"build": "pnpm run clean && tsc -p tsconfig.build.json && node build-css.mjs",
|
|
49
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
50
|
+
"test": "vitest run"
|
|
51
|
+
}
|
|
52
|
+
}
|