@cloudwick/astral-ui-cli 2.4.0 → 2.5.1
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/dist/index.js +776 -503
- package/dist/index.js.map +1 -1
- package/dist/templates/constants/headingLevel.ts +4 -0
- package/dist/templates/constants/index.ts +1 -0
- package/dist/templates/hooks/index.ts +4 -0
- package/dist/templates/hooks/useDeprecationWarning.ts +25 -0
- package/dist/templates/hooks/useLiveAnnouncement.ts +23 -0
- package/dist/templates/hooks/usePartRegistration.ts +27 -0
- package/dist/templates/hooks/useSelectionLimit.ts +75 -0
- package/dist/templates/utils/devWarn.ts +16 -3
- package/dist/templates/utils/elementUtils.ts +40 -0
- package/dist/templates/utils/index.ts +1 -0
- package/dist/templates/utils/optionText.ts +9 -0
- package/dist/templates/utils/stringUtils/index.tsx +2 -0
- package/dist/templates/utils/stringUtils/optionToggled.ts +14 -0
- package/dist/templates/utils/stringUtils/selectionLimitReached.ts +10 -0
- package/package.json +1 -1
|
@@ -19,3 +19,7 @@ export { useOnValueChange } from "./useOnValueChange";
|
|
|
19
19
|
export { useIsTruncated } from "./useIsTruncated";
|
|
20
20
|
export { useListboxKeyboardNav } from "./useListboxKeyboardNav";
|
|
21
21
|
export { useLabelPeekDismissal } from "./useLabelPeekDismissal";
|
|
22
|
+
export { usePartPresence, usePartRegistration } from "./usePartRegistration";
|
|
23
|
+
export { useLiveAnnouncement, type ILiveAnnouncer } from "./useLiveAnnouncement";
|
|
24
|
+
export { useSelectionLimit, resolveSelectionLimit } from "./useSelectionLimit";
|
|
25
|
+
export { useDeprecationWarning, useStringIconDeprecation } from "./useDeprecationWarning";
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
|
|
3
|
+
import { devWarn } from "@utils";
|
|
4
|
+
|
|
5
|
+
// Module-scoped: each message prints once per page load, however many instances render it.
|
|
6
|
+
const warned = new Set<string>();
|
|
7
|
+
|
|
8
|
+
/** Warns once per page load, per message, while `active`. */
|
|
9
|
+
export function useDeprecationWarning( active: boolean, message: string ): void {
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
if ( active && !warned.has( message )) {
|
|
12
|
+
warned.add( message );
|
|
13
|
+
devWarn( message );
|
|
14
|
+
}
|
|
15
|
+
}, [ active, message ]);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Warns once per page load when `component` receives a string icon name in `prefixIcon` or `suffixIcon`. */
|
|
19
|
+
export function useStringIconDeprecation( component: string, prefixIcon: unknown, suffixIcon: unknown ): void {
|
|
20
|
+
useDeprecationWarning(
|
|
21
|
+
typeof prefixIcon === "string" || typeof suffixIcon === "string",
|
|
22
|
+
`${component}: string icons in \`prefixIcon\` / \`suffixIcon\` are deprecated and will be removed in the ` +
|
|
23
|
+
"next major. Install the `@cloudwick/icons` npm package and pass one of its icons instead."
|
|
24
|
+
);
|
|
25
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useCallback, useState } from "react";
|
|
2
|
+
|
|
3
|
+
export interface ILiveAnnouncer {
|
|
4
|
+
/** Text for a polite live region. */
|
|
5
|
+
message: string;
|
|
6
|
+
/** Replaces the message. A repeat of the current one is still announced. */
|
|
7
|
+
announce: ( message: string ) => void;
|
|
8
|
+
/** Empties the region. */
|
|
9
|
+
clear: () => void;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* State for a polite live region. Screen readers announce a region only when its text changes, so a
|
|
14
|
+
* repeat of the current message toggles a trailing no-break space to be read again.
|
|
15
|
+
*/
|
|
16
|
+
export function useLiveAnnouncement(): ILiveAnnouncer {
|
|
17
|
+
const [ message, setMessage ] = useState( "" );
|
|
18
|
+
const announce = useCallback(( next: string ) => {
|
|
19
|
+
setMessage(( prev ) => ( prev === next ? `${next}\u00A0` : next ));
|
|
20
|
+
}, []);
|
|
21
|
+
const clear = useCallback(() => setMessage( "" ), []);
|
|
22
|
+
return { message, announce, clear };
|
|
23
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { useCallback, useLayoutEffect, useState } from "react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Tracks whether any instance of a compound component's part (a title, a header) is rendered. Parts
|
|
5
|
+
* report themselves through `usePartRegistration`, which also finds parts nested in fragments or
|
|
6
|
+
* wrapper components. Instances are counted, so one unmounting doesn't hide another still rendered.
|
|
7
|
+
*/
|
|
8
|
+
export function usePartPresence(): [ boolean, ( mounted: boolean ) => void ] {
|
|
9
|
+
const [ count, setCount ] = useState( 0 );
|
|
10
|
+
const register = useCallback(( mounted: boolean ) => {
|
|
11
|
+
setCount(( current ) => current + ( mounted ? 1 : -1 ));
|
|
12
|
+
}, []);
|
|
13
|
+
return [ count > 0, register ];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Reports a part to the `register` callback from `usePartPresence` while it renders content.
|
|
18
|
+
*/
|
|
19
|
+
export function usePartRegistration( register: (( mounted: boolean ) => void ) | undefined, hasContent: boolean ): void {
|
|
20
|
+
useLayoutEffect(() => {
|
|
21
|
+
if ( !register || !hasContent ) {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
register( true );
|
|
25
|
+
return () => register( false );
|
|
26
|
+
}, [ register, hasContent ]);
|
|
27
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { useCallback } from "react";
|
|
2
|
+
|
|
3
|
+
import { formatSelectionLimitReached } from "@utils";
|
|
4
|
+
import { useIgnoredPropWarning } from "./useIgnoredPropWarning";
|
|
5
|
+
|
|
6
|
+
interface IUseSelectionLimitArgs {
|
|
7
|
+
/** The consumer's `maxSelected`. Anything but a positive integer is ignored (dev warning). */
|
|
8
|
+
maxSelected?: number;
|
|
9
|
+
/** The consumer's override for the announcement. */
|
|
10
|
+
maxSelectedMessage?: string;
|
|
11
|
+
/** The consumer's `showSelectAll`. */
|
|
12
|
+
showSelectAll: boolean;
|
|
13
|
+
/** How many options the selection being built currently holds. */
|
|
14
|
+
count: number;
|
|
15
|
+
/** Prefix for the dev warning. */
|
|
16
|
+
componentName: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface IUseSelectionLimitResult {
|
|
20
|
+
/** The validated limit; `undefined` when none applies. */
|
|
21
|
+
max: number | undefined;
|
|
22
|
+
/** The selection holds `max` or more options, so nothing more may be added. */
|
|
23
|
+
isCapped: boolean;
|
|
24
|
+
/** "Select all" may render: the consumer allows it and no limit applies, since "all" could exceed one. */
|
|
25
|
+
allowSelectAll: boolean;
|
|
26
|
+
/** The limit notice (`maxSelectedMessage` or the default wording); `undefined` when no limit applies. */
|
|
27
|
+
limitMessage: string | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* `message` for a user action that takes the selection from `count` to `nextCount`, followed by the
|
|
30
|
+
* limit notice when that step reaches the limit. One region then speaks both, and a selection that
|
|
31
|
+
* changes on its own (a parent loading a saved value) stays quiet.
|
|
32
|
+
*/
|
|
33
|
+
withLimitNotice: ( message: string, nextCount: number ) => string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** `maxSelected` as a usable limit: a positive integer, otherwise `undefined` (no limit). */
|
|
37
|
+
export const resolveSelectionLimit = ( max: number | undefined ): number | undefined =>
|
|
38
|
+
max !== undefined && Number.isInteger( max ) && max > 0 ? max : undefined;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The `maxSelected` / `showSelectAll` contract shared by the multi-selection components: validates the
|
|
42
|
+
* limit, reports whether the selection is at it, decides whether "Select all" may render, and words the
|
|
43
|
+
* notice for the step that reaches it.
|
|
44
|
+
*/
|
|
45
|
+
export function useSelectionLimit({
|
|
46
|
+
maxSelected,
|
|
47
|
+
maxSelectedMessage,
|
|
48
|
+
showSelectAll,
|
|
49
|
+
count,
|
|
50
|
+
componentName
|
|
51
|
+
}: IUseSelectionLimitArgs ): IUseSelectionLimitResult {
|
|
52
|
+
const max = resolveSelectionLimit( maxSelected );
|
|
53
|
+
useIgnoredPropWarning(
|
|
54
|
+
maxSelected !== undefined && max === undefined,
|
|
55
|
+
`${componentName}: \`maxSelected\` must be a positive integer; ${String( maxSelected )} is ignored.`
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
const limitMessage = max === undefined ? undefined : maxSelectedMessage ?? formatSelectionLimitReached( max );
|
|
59
|
+
|
|
60
|
+
const withLimitNotice = useCallback(
|
|
61
|
+
( message: string, nextCount: number ): string => {
|
|
62
|
+
const reachesLimit = max !== undefined && count < max && nextCount >= max;
|
|
63
|
+
return [ message, reachesLimit ? limitMessage : "" ].filter( Boolean ).join( ". " );
|
|
64
|
+
},
|
|
65
|
+
[ max, count, limitMessage ]
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
max,
|
|
70
|
+
isCapped: max !== undefined && count >= max,
|
|
71
|
+
allowSelectAll: showSelectAll && max === undefined,
|
|
72
|
+
limitMessage,
|
|
73
|
+
withLimitNotice
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -1,9 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* Whether `process.env.NODE_ENV` is anything other than `"production"`.
|
|
3
|
+
*
|
|
4
|
+
* The published build leaves `process.env.NODE_ENV` in place for the consumer's bundler to
|
|
5
|
+
* replace; bundlers substitute the whole expression and leave `process` itself undefined in the
|
|
6
|
+
* browser, so the expression is read directly rather than behind a `typeof process` check.
|
|
7
|
+
* Unbundled, reading it throws a `ReferenceError`, which is treated as production.
|
|
4
8
|
*/
|
|
9
|
+
export function isDevEnvironment(): boolean {
|
|
10
|
+
try {
|
|
11
|
+
return process.env.NODE_ENV !== "production";
|
|
12
|
+
} catch {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Logs `message` with `console.warn` unless `process.env.NODE_ENV` is `"production"`. */
|
|
5
18
|
export function devWarn( message: string ): void {
|
|
6
|
-
if (
|
|
19
|
+
if ( isDevEnvironment()) {
|
|
7
20
|
console.warn( message );
|
|
8
21
|
}
|
|
9
22
|
}
|
|
@@ -24,3 +24,43 @@ export const isSemanticTrigger = ( ctaContent: React.ReactNode ): boolean => {
|
|
|
24
24
|
semanticInteractiveElements.includes( ctaContent.type )
|
|
25
25
|
);
|
|
26
26
|
};
|
|
27
|
+
|
|
28
|
+
const NAMING_ATTRIBUTES = [ "aria-label", "aria-labelledby" ];
|
|
29
|
+
const UNNAMEABLE_ROLES = [ "none", "presentation", "generic" ];
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Whether an element can be named by `aria-label`/`aria-labelledby` or `alt`: one with an explicit role,
|
|
33
|
+
* or a native control (`button`, `a`, `input`...). A role-less `span` or `div` cannot: ARIA prohibits
|
|
34
|
+
* naming generic elements, so screen readers ignore the label.
|
|
35
|
+
*/
|
|
36
|
+
const hasUsableName = ( element: Element ): boolean => {
|
|
37
|
+
if ( element.localName === "img" ) {
|
|
38
|
+
return Boolean( element.getAttribute( "alt" )?.trim());
|
|
39
|
+
}
|
|
40
|
+
const role = element.getAttribute( "role" );
|
|
41
|
+
const canBeNamed = role ? !UNNAMEABLE_ROLES.includes( role ) : semanticInteractiveElements.includes( element.localName );
|
|
42
|
+
return canBeNamed &&
|
|
43
|
+
NAMING_ATTRIBUTES.some(( attribute ) => element.getAttribute( attribute )?.trim());
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Whether an element contains anything a screen reader reads from its content. Text counts unless it
|
|
48
|
+
* sits in an `aria-hidden` subtree or an SVG (such as an icon's `<title>`). With `countNamedElements`,
|
|
49
|
+
* a descendant with a role or a native control that is named by `aria-label`/`aria-labelledby`, or an
|
|
50
|
+
* `img` with `alt`, also counts.
|
|
51
|
+
*/
|
|
52
|
+
export const hasExposedText = ( element: Element, { countNamedElements = false } = {}): boolean => {
|
|
53
|
+
for ( let node = element.firstChild; node; node = node.nextSibling ) {
|
|
54
|
+
if ( node.nodeType === Node.TEXT_NODE && node.textContent?.trim()) {
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
// A nodeType check rather than `instanceof Element`, which fails for nodes from another document's realm.
|
|
58
|
+
const child = node.nodeType === Node.ELEMENT_NODE ? node as Element : null;
|
|
59
|
+
if ( child && child.getAttribute( "aria-hidden" ) !== "true" ) {
|
|
60
|
+
if (( countNamedElements && hasUsableName( child )) || ( child.localName !== "svg" && hasExposedText( child, { countNamedElements }))) {
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return false;
|
|
66
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The plain text an option is known by when its label has to be spoken or shown as text (a tooltip,
|
|
5
|
+
* an announcement): a string `label`, else `searchValue`, else the stringified `value`.
|
|
6
|
+
*/
|
|
7
|
+
export function getOptionText( option: { label: ReactNode; value: unknown; searchValue?: string }): string {
|
|
8
|
+
return typeof option.label === "string" ? option.label : option.searchValue ?? String( option.value );
|
|
9
|
+
}
|
|
@@ -2,3 +2,5 @@ export { readableFileSize } from "./fileSize";
|
|
|
2
2
|
export { isRegexSafe } from "./isRegexSafe";
|
|
3
3
|
export { joinIds } from "./joinIds";
|
|
4
4
|
export { formatResultsAvailable } from "./resultsAvailable";
|
|
5
|
+
export { formatSelectionLimitReached } from "./selectionLimitReached";
|
|
6
|
+
export { formatOptionToggled } from "./optionToggled";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The status-region text for an option the user just ticked or unticked. Spoken because a toggle leaves
|
|
3
|
+
* focus where it was, and screen readers do not reliably announce an `aria-selected` change on the
|
|
4
|
+
* option `aria-activedescendant` points at.
|
|
5
|
+
* @param label - the option's plain text
|
|
6
|
+
* @param selected - whether the option is now selected
|
|
7
|
+
* @returns {string} - the announcement
|
|
8
|
+
* @example
|
|
9
|
+
* formatOptionToggled("Apple", true) // returns "Apple, selected"
|
|
10
|
+
* formatOptionToggled("Apple", false) // returns "Apple, not selected"
|
|
11
|
+
*/
|
|
12
|
+
export function formatOptionToggled( label: string, selected: boolean ): string {
|
|
13
|
+
return `${label}, ${selected ? "selected" : "not selected"}`;
|
|
14
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The status-region text a multi-selection control announces once its selection limit is reached.
|
|
3
|
+
* @param max - the most options that may be selected
|
|
4
|
+
* @returns {string} - the announcement
|
|
5
|
+
* @example
|
|
6
|
+
* formatSelectionLimitReached(3) // returns "Selection limit of 3 reached"
|
|
7
|
+
*/
|
|
8
|
+
export function formatSelectionLimitReached( max: number ): string {
|
|
9
|
+
return `Selection limit of ${max} reached`;
|
|
10
|
+
}
|