@feitio/react 0.0.0-stage → 0.2.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 +21 -0
- package/README.md +35 -2
- package/dist/Anchor.d.ts +19 -0
- package/dist/AppShell.d.ts +24 -0
- package/dist/BackToTop.d.ts +8 -0
- package/dist/Block.d.ts +7 -0
- package/dist/Breadcrumbs.d.ts +12 -0
- package/dist/Button.d.ts +16 -0
- package/dist/Checkbox.d.ts +11 -0
- package/dist/CodeBlock.d.ts +10 -0
- package/dist/Details.d.ts +9 -0
- package/dist/Dialog.d.ts +21 -0
- package/dist/Divider.d.ts +7 -0
- package/dist/DoDont.d.ts +14 -0
- package/dist/Drawer.d.ts +13 -0
- package/dist/EmptyState.d.ts +11 -0
- package/dist/ErrorMessage.d.ts +8 -0
- package/dist/ErrorSummary.d.ts +11 -0
- package/dist/Example.d.ts +14 -0
- package/dist/FeitioProvider.d.ts +8 -0
- package/dist/Field.d.ts +15 -0
- package/dist/Fieldset.d.ts +8 -0
- package/dist/FilterField.d.ts +19 -0
- package/dist/Footer.d.ts +14 -0
- package/dist/Grid.d.ts +10 -0
- package/dist/Header.d.ts +22 -0
- package/dist/Heading.d.ts +8 -0
- package/dist/Hint.d.ts +7 -0
- package/dist/Icon.d.ts +9 -0
- package/dist/Inline.d.ts +9 -0
- package/dist/Kbd.d.ts +5 -0
- package/dist/Label.d.ts +8 -0
- package/dist/Link.d.ts +11 -0
- package/dist/LinkCard.d.ts +9 -0
- package/dist/Menu.d.ts +25 -0
- package/dist/Nav.d.ts +17 -0
- package/dist/Notice.d.ts +19 -0
- package/dist/Pagination.d.ts +20 -0
- package/dist/Radios.d.ts +13 -0
- package/dist/ScrollFade.d.ts +3 -0
- package/dist/Search.d.ts +17 -0
- package/dist/SegmentedControl.d.ts +9 -0
- package/dist/Select.d.ts +16 -0
- package/dist/SkipLink.d.ts +6 -0
- package/dist/Stack.d.ts +9 -0
- package/dist/StatusOverlay.d.ts +12 -0
- package/dist/SummaryList.d.ts +13 -0
- package/dist/Swatch.d.ts +5 -0
- package/dist/Switch.d.ts +12 -0
- package/dist/Table.d.ts +18 -0
- package/dist/Tag.d.ts +9 -0
- package/dist/TaskList.d.ts +13 -0
- package/dist/Text.d.ts +5 -0
- package/dist/TextInput.d.ts +10 -0
- package/dist/Textarea.d.ts +8 -0
- package/dist/TimeInput.d.ts +14 -0
- package/dist/VisuallyHidden.d.ts +5 -0
- package/dist/actions/notices.d.ts +16 -0
- package/dist/actions/status.d.ts +5 -0
- package/dist/actions/use-element-behavior.d.ts +6 -0
- package/dist/actions/use-page-behavior.d.ts +6 -0
- package/dist/actions/use-sprite.d.ts +2 -0
- package/dist/actions/wire.d.ts +10 -0
- package/dist/calculations/pagination.d.ts +10 -0
- package/dist/calculations/svg-parts.d.ts +9 -0
- package/dist/calculations/text.d.ts +4 -0
- package/dist/data/sprite.d.ts +4 -0
- package/dist/index.d.ts +58 -0
- package/dist/index.js +2482 -0
- package/package.json +56 -4
- package/src/Anchor.tsx +47 -0
- package/src/AppShell.tsx +95 -0
- package/src/BackToTop.tsx +26 -0
- package/src/Block.tsx +11 -0
- package/src/Breadcrumbs.tsx +40 -0
- package/src/Button.tsx +40 -0
- package/src/Checkbox.tsx +34 -0
- package/src/CodeBlock.tsx +38 -0
- package/src/Details.tsx +22 -0
- package/src/Dialog.tsx +70 -0
- package/src/Divider.tsx +17 -0
- package/src/DoDont.tsx +46 -0
- package/src/Drawer.tsx +52 -0
- package/src/EmptyState.tsx +37 -0
- package/src/ErrorMessage.tsx +18 -0
- package/src/ErrorSummary.tsx +27 -0
- package/src/Example.tsx +45 -0
- package/src/FeitioProvider.tsx +13 -0
- package/src/Field.tsx +45 -0
- package/src/Fieldset.tsx +18 -0
- package/src/FilterField.tsx +61 -0
- package/src/Footer.tsx +37 -0
- package/src/Grid.tsx +19 -0
- package/src/Header.tsx +60 -0
- package/src/Heading.tsx +13 -0
- package/src/Hint.tsx +15 -0
- package/src/Icon.tsx +19 -0
- package/src/Inline.tsx +17 -0
- package/src/Kbd.tsx +6 -0
- package/src/Label.tsx +16 -0
- package/src/Link.tsx +33 -0
- package/src/LinkCard.tsx +22 -0
- package/src/Menu.tsx +73 -0
- package/src/Nav.tsx +36 -0
- package/src/Notice.tsx +73 -0
- package/src/Pagination.tsx +98 -0
- package/src/Radios.tsx +43 -0
- package/src/ScrollFade.tsx +10 -0
- package/src/Search.tsx +66 -0
- package/src/SegmentedControl.tsx +36 -0
- package/src/Select.tsx +50 -0
- package/src/SkipLink.tsx +16 -0
- package/src/Stack.tsx +17 -0
- package/src/StatusOverlay.tsx +46 -0
- package/src/SummaryList.tsx +27 -0
- package/src/Swatch.tsx +11 -0
- package/src/Switch.tsx +41 -0
- package/src/Table.tsx +74 -0
- package/src/Tag.tsx +21 -0
- package/src/TaskList.tsx +34 -0
- package/src/Text.tsx +6 -0
- package/src/TextInput.tsx +29 -0
- package/src/Textarea.tsx +25 -0
- package/src/TimeInput.tsx +59 -0
- package/src/VisuallyHidden.tsx +6 -0
- package/src/actions/notices.ts +30 -0
- package/src/actions/status.ts +13 -0
- package/src/actions/use-element-behavior.ts +23 -0
- package/src/actions/use-page-behavior.ts +17 -0
- package/src/actions/use-sprite.ts +8 -0
- package/src/actions/wire.ts +30 -0
- package/src/calculations/pagination.ts +15 -0
- package/src/calculations/svg-parts.ts +22 -0
- package/src/calculations/text.ts +9 -0
- package/src/data/sprite.ts +7 -0
- package/src/index.ts +59 -0
- package/src/text-modules.d.ts +5 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { ReactElement } from 'react';
|
|
2
|
+
import { usePageBehavior } from './actions/use-page-behavior.ts';
|
|
3
|
+
import { wireForms } from './actions/wire.ts';
|
|
4
|
+
|
|
5
|
+
export interface TimeInputProps {
|
|
6
|
+
hour?: string;
|
|
7
|
+
minute?: string;
|
|
8
|
+
cycle?: '12' | '24';
|
|
9
|
+
period?: 'AM' | 'PM';
|
|
10
|
+
hourLabel?: string;
|
|
11
|
+
minuteLabel?: string;
|
|
12
|
+
periodLabel?: string;
|
|
13
|
+
invalid?: boolean;
|
|
14
|
+
disabled?: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** TimeInput: an hour and its minutes in two short fields; in a 12-hour cycle, AM or PM. */
|
|
18
|
+
export function TimeInput({
|
|
19
|
+
hour = '09',
|
|
20
|
+
minute = '00',
|
|
21
|
+
cycle = '24',
|
|
22
|
+
period = 'AM',
|
|
23
|
+
hourLabel = 'Hora',
|
|
24
|
+
minuteLabel = 'Minuto',
|
|
25
|
+
periodLabel = 'AM ou PM',
|
|
26
|
+
invalid,
|
|
27
|
+
disabled,
|
|
28
|
+
}: TimeInputProps): ReactElement {
|
|
29
|
+
usePageBehavior('feitioForms', wireForms);
|
|
30
|
+
return (
|
|
31
|
+
<div className="feitio-time-field" data-hour-cycle={cycle}>
|
|
32
|
+
<input
|
|
33
|
+
className="seg"
|
|
34
|
+
inputMode="numeric"
|
|
35
|
+
maxLength={2}
|
|
36
|
+
defaultValue={hour}
|
|
37
|
+
aria-label={hourLabel}
|
|
38
|
+
aria-invalid={invalid ? 'true' : undefined}
|
|
39
|
+
disabled={disabled}
|
|
40
|
+
/>
|
|
41
|
+
<span className="sep" aria-hidden="true">
|
|
42
|
+
:
|
|
43
|
+
</span>
|
|
44
|
+
<input
|
|
45
|
+
className="seg"
|
|
46
|
+
inputMode="numeric"
|
|
47
|
+
maxLength={2}
|
|
48
|
+
defaultValue={minute}
|
|
49
|
+
aria-label={minuteLabel}
|
|
50
|
+
disabled={disabled}
|
|
51
|
+
/>
|
|
52
|
+
{cycle === '12' && (
|
|
53
|
+
<button type="button" className="period" aria-label={periodLabel} disabled={disabled}>
|
|
54
|
+
{period}
|
|
55
|
+
</button>
|
|
56
|
+
)}
|
|
57
|
+
</div>
|
|
58
|
+
);
|
|
59
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { ReactElement, ReactNode } from 'react';
|
|
2
|
+
|
|
3
|
+
/** VisuallyHidden: words only screen readers hear. */
|
|
4
|
+
export function VisuallyHidden({ children }: { children?: ReactNode }): ReactElement {
|
|
5
|
+
return <span className="feitio-visually-hidden">{children}</span>;
|
|
6
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { notices } from '@feitio/vanilla/notice/source';
|
|
2
|
+
import { useCallback } from 'react';
|
|
3
|
+
|
|
4
|
+
/** What the page's Notice controller does. */
|
|
5
|
+
export interface NoticeApi {
|
|
6
|
+
show: (notice: HTMLElement) => void;
|
|
7
|
+
close: (notice: HTMLElement) => void;
|
|
8
|
+
wire: (root: ParentNode) => void;
|
|
9
|
+
}
|
|
10
|
+
type Page = Window & { feitioNotice?: NoticeApi };
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The page's Notice controller, the same one the Astro Notice creates (window.feitioNotice); made
|
|
14
|
+
* here first, it wires the notices already on the page, as the Astro script would.
|
|
15
|
+
*/
|
|
16
|
+
export function noticeApi(window: Window): NoticeApi {
|
|
17
|
+
const page = window as Page;
|
|
18
|
+
page.feitioNotice ??= notices(window);
|
|
19
|
+
return page.feitioNotice;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Show and close a Notice from code: show(element) brings a hidden notice in, close(element) takes it out. */
|
|
23
|
+
export function useNotices(): {
|
|
24
|
+
show: (notice: HTMLElement) => void;
|
|
25
|
+
close: (notice: HTMLElement) => void;
|
|
26
|
+
} {
|
|
27
|
+
const show = useCallback((notice: HTMLElement) => noticeApi(window).show(notice), []);
|
|
28
|
+
const close = useCallback((notice: HTMLElement) => noticeApi(window).close(notice), []);
|
|
29
|
+
return { show, close };
|
|
30
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { status as setStatus } from '@feitio/vanilla/status/source';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Shows a StatusOverlay's feedback from code, on its element: "loading", "ok" or "error", with the
|
|
5
|
+
* words for it; null clears it. In React, the state and label props usually do this.
|
|
6
|
+
*/
|
|
7
|
+
export function status(
|
|
8
|
+
host: HTMLElement,
|
|
9
|
+
state: 'loading' | 'ok' | 'error' | null,
|
|
10
|
+
label = '',
|
|
11
|
+
): void {
|
|
12
|
+
setStatus(host, state, label);
|
|
13
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type RefObject, useEffect } from 'react';
|
|
2
|
+
|
|
3
|
+
const wired = new WeakMap<Element, Set<string>>();
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Wires a behavior on one element when it mounts, only once per element (React's strict mode runs
|
|
7
|
+
* effects twice in development; listeners must not double).
|
|
8
|
+
*/
|
|
9
|
+
export function useElementBehavior<T extends Element>(
|
|
10
|
+
ref: RefObject<T | null>,
|
|
11
|
+
name: string,
|
|
12
|
+
wire: (element: T) => void,
|
|
13
|
+
): void {
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
const element = ref.current;
|
|
16
|
+
if (!element) return;
|
|
17
|
+
const names = wired.get(element) ?? new Set<string>();
|
|
18
|
+
if (names.has(name)) return;
|
|
19
|
+
names.add(name);
|
|
20
|
+
wired.set(element, names);
|
|
21
|
+
wire(element);
|
|
22
|
+
}, [ref, name, wire]);
|
|
23
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useEffect } from 'react';
|
|
2
|
+
|
|
3
|
+
type Page = Window & Record<string, unknown>;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Wires a delegated behavior once for the whole page (it listens on the document, so components that
|
|
7
|
+
* mount later are covered too). The flag on window is shared with the Astro components, so a page
|
|
8
|
+
* that mixes both never wires it twice.
|
|
9
|
+
*/
|
|
10
|
+
export function usePageBehavior(flag: string, wire: (window: Window) => void): void {
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
const page = window as unknown as Page;
|
|
13
|
+
if (page[flag]) return;
|
|
14
|
+
page[flag] = true;
|
|
15
|
+
wire(window);
|
|
16
|
+
}, [flag, wire]);
|
|
17
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { useContext } from 'react';
|
|
2
|
+
import { DEFAULT_SPRITE, SpriteContext } from '../data/sprite.ts';
|
|
3
|
+
|
|
4
|
+
/** The sprite an icon uses: its own, else the FeitioProvider's, else the default path. */
|
|
5
|
+
export function useSprite(own?: string): string {
|
|
6
|
+
const shared = useContext(SpriteContext);
|
|
7
|
+
return own ?? shared ?? DEFAULT_SPRITE;
|
|
8
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { dialogs } from '@feitio/vanilla/dialog/source';
|
|
2
|
+
import { forms } from '@feitio/vanilla/forms/source';
|
|
3
|
+
import { menus } from '@feitio/vanilla/menu/source';
|
|
4
|
+
import { scrolls } from '@feitio/vanilla/scroll/source';
|
|
5
|
+
import { noticeApi } from './notices.ts';
|
|
6
|
+
|
|
7
|
+
/** The form controls of the page: Switch words, SegmentedControl choice, AM/PM, FilterField. */
|
|
8
|
+
export function wireForms(window: Window): void {
|
|
9
|
+
forms(window);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** The Dialogs of the page: openers, the typed confirmation, the reset on close. */
|
|
13
|
+
export function wireDialogs(window: Window): void {
|
|
14
|
+
dialogs(window);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** One Menu: its button, its list, the arrows. */
|
|
18
|
+
export function wireMenu(element: HTMLElement): void {
|
|
19
|
+
menus(window, element);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** One Notice: its close button and, when timed, its clock. */
|
|
23
|
+
export function wireNotice(element: HTMLElement): void {
|
|
24
|
+
noticeApi(window).wire(element);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** The ScrollFade or BackToTop next to this element (each one is wired once). */
|
|
28
|
+
export function wireScroll(element: HTMLElement): void {
|
|
29
|
+
scrolls(window, element.parentElement ?? element);
|
|
30
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** One entry of a Pagination: a page number, or a gap (…) between pages that are not next to each other. */
|
|
2
|
+
export type PageEntry = { kind: 'page'; page: number } | { kind: 'gap'; after: number };
|
|
3
|
+
|
|
4
|
+
/** The pages a Pagination shows: the first, the last, and the ones around the current, with gaps between. */
|
|
5
|
+
export function paginationEntries(current: number, total: number): PageEntry[] {
|
|
6
|
+
const around = [current - 1, current, current + 1].filter((p) => p >= 1 && p <= total);
|
|
7
|
+
const pages = [...new Set([1, ...around, total])].sort((a, b) => a - b);
|
|
8
|
+
const entries: PageEntry[] = [];
|
|
9
|
+
pages.forEach((page, i) => {
|
|
10
|
+
const before = pages[i - 1];
|
|
11
|
+
if (before !== undefined && page - before > 1) entries.push({ kind: 'gap', after: before });
|
|
12
|
+
entries.push({ kind: 'page', page });
|
|
13
|
+
});
|
|
14
|
+
return entries;
|
|
15
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** An SVG file split for React: its root attributes, named as React wants them, and its inner markup. */
|
|
2
|
+
export interface SvgParts {
|
|
3
|
+
attributes: Record<string, string>;
|
|
4
|
+
inner: string;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/** stroke-width becomes strokeWidth; aria-*, data-* and names without a dash stay as they are. */
|
|
8
|
+
export function reactAttributeName(name: string): string {
|
|
9
|
+
if (name.startsWith('aria-') || name.startsWith('data-')) return name;
|
|
10
|
+
if (name === 'class') return 'className';
|
|
11
|
+
return name.replace(/-([a-z])/g, (_, letter: string) => letter.toUpperCase());
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Splits an SVG file's text into its root's attributes and what is inside it. */
|
|
15
|
+
export function svgParts(svg: string): SvgParts {
|
|
16
|
+
const match = /<svg\b([^>]*)>([\s\S]*)<\/svg>/.exec(svg);
|
|
17
|
+
if (!match) return { attributes: {}, inner: '' };
|
|
18
|
+
const attributes: Record<string, string> = {};
|
|
19
|
+
for (const [, name, value] of (match[1] ?? '').matchAll(/([\w:-]+)="([^"]*)"/g))
|
|
20
|
+
if (name !== undefined) attributes[reactAttributeName(name)] = value ?? '';
|
|
21
|
+
return { attributes, inner: (match[2] ?? '').trim() };
|
|
22
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Joins class names, leaving out the empty ones. */
|
|
2
|
+
export function classNames(...names: (string | false | null | undefined)[]): string {
|
|
3
|
+
return names.filter(Boolean).join(' ');
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
/** The years a Footer shows: the year it started, or "since–now" when that was before this year. */
|
|
7
|
+
export function yearsText(since: number, year: number): string {
|
|
8
|
+
return since === year ? String(since) : `${since}–${year}`;
|
|
9
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { createContext } from 'react';
|
|
2
|
+
|
|
3
|
+
/** Where the icon sprite is served when nobody says otherwise: the same default as the Astro Icon. */
|
|
4
|
+
export const DEFAULT_SPRITE = '/feitio/icons/sprite.svg';
|
|
5
|
+
|
|
6
|
+
/** The sprite for every icon under a FeitioProvider. */
|
|
7
|
+
export const SpriteContext = createContext<string | undefined>(undefined);
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/** Feitio for React: the same HTML as the Astro components, with the behaviors wired by hooks. */
|
|
2
|
+
|
|
3
|
+
export * from './Anchor.tsx';
|
|
4
|
+
export * from './AppShell.tsx';
|
|
5
|
+
export { useNotices } from './actions/notices.ts';
|
|
6
|
+
export { status } from './actions/status.ts';
|
|
7
|
+
export * from './BackToTop.tsx';
|
|
8
|
+
export * from './Block.tsx';
|
|
9
|
+
export * from './Breadcrumbs.tsx';
|
|
10
|
+
export * from './Button.tsx';
|
|
11
|
+
export * from './Checkbox.tsx';
|
|
12
|
+
export * from './CodeBlock.tsx';
|
|
13
|
+
export * from './Details.tsx';
|
|
14
|
+
export * from './Dialog.tsx';
|
|
15
|
+
export * from './Divider.tsx';
|
|
16
|
+
export * from './DoDont.tsx';
|
|
17
|
+
export * from './Drawer.tsx';
|
|
18
|
+
export * from './EmptyState.tsx';
|
|
19
|
+
export * from './ErrorMessage.tsx';
|
|
20
|
+
export * from './ErrorSummary.tsx';
|
|
21
|
+
export * from './Example.tsx';
|
|
22
|
+
export * from './FeitioProvider.tsx';
|
|
23
|
+
export * from './Field.tsx';
|
|
24
|
+
export * from './Fieldset.tsx';
|
|
25
|
+
export * from './FilterField.tsx';
|
|
26
|
+
export * from './Footer.tsx';
|
|
27
|
+
export * from './Grid.tsx';
|
|
28
|
+
export * from './Header.tsx';
|
|
29
|
+
export * from './Heading.tsx';
|
|
30
|
+
export * from './Hint.tsx';
|
|
31
|
+
export * from './Icon.tsx';
|
|
32
|
+
export * from './Inline.tsx';
|
|
33
|
+
export * from './Kbd.tsx';
|
|
34
|
+
export * from './Label.tsx';
|
|
35
|
+
export * from './Link.tsx';
|
|
36
|
+
export * from './LinkCard.tsx';
|
|
37
|
+
export * from './Menu.tsx';
|
|
38
|
+
export * from './Nav.tsx';
|
|
39
|
+
export * from './Notice.tsx';
|
|
40
|
+
export * from './Pagination.tsx';
|
|
41
|
+
export * from './Radios.tsx';
|
|
42
|
+
export * from './ScrollFade.tsx';
|
|
43
|
+
export * from './Search.tsx';
|
|
44
|
+
export * from './SegmentedControl.tsx';
|
|
45
|
+
export * from './Select.tsx';
|
|
46
|
+
export * from './SkipLink.tsx';
|
|
47
|
+
export * from './Stack.tsx';
|
|
48
|
+
export * from './StatusOverlay.tsx';
|
|
49
|
+
export * from './SummaryList.tsx';
|
|
50
|
+
export * from './Swatch.tsx';
|
|
51
|
+
export * from './Switch.tsx';
|
|
52
|
+
export * from './Table.tsx';
|
|
53
|
+
export * from './Tag.tsx';
|
|
54
|
+
export * from './TaskList.tsx';
|
|
55
|
+
export * from './Text.tsx';
|
|
56
|
+
export * from './Textarea.tsx';
|
|
57
|
+
export * from './TextInput.tsx';
|
|
58
|
+
export * from './TimeInput.tsx';
|
|
59
|
+
export * from './VisuallyHidden.tsx';
|