@cdx-ui/primitives 0.3.4 → 0.3.6
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/lib/commonjs/index.js +12 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/select/SelectEmpty.js +2 -13
- package/lib/commonjs/select/SelectEmpty.js.map +1 -1
- package/lib/commonjs/slider/useSliderPointer.js +9 -4
- package/lib/commonjs/slider/useSliderPointer.js.map +1 -1
- package/lib/commonjs/stepper/StepperRoot.js +119 -0
- package/lib/commonjs/stepper/StepperRoot.js.map +1 -0
- package/lib/commonjs/stepper/StepperStep.js +78 -0
- package/lib/commonjs/stepper/StepperStep.js.map +1 -0
- package/lib/commonjs/stepper/StepperStepPressable.js +63 -0
- package/lib/commonjs/stepper/StepperStepPressable.js.map +1 -0
- package/lib/commonjs/stepper/StepperStepTrigger.js +44 -0
- package/lib/commonjs/stepper/StepperStepTrigger.js.map +1 -0
- package/lib/commonjs/stepper/StepperStepTrigger.web.js +71 -0
- package/lib/commonjs/stepper/StepperStepTrigger.web.js.map +1 -0
- package/lib/commonjs/stepper/context.js +21 -0
- package/lib/commonjs/stepper/context.js.map +1 -0
- package/lib/commonjs/stepper/index.js +46 -0
- package/lib/commonjs/stepper/index.js.map +1 -0
- package/lib/commonjs/stepper/types.js +6 -0
- package/lib/commonjs/stepper/types.js.map +1 -0
- package/lib/module/index.js +1 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/select/SelectEmpty.js +2 -13
- package/lib/module/select/SelectEmpty.js.map +1 -1
- package/lib/module/slider/useSliderPointer.js +9 -4
- package/lib/module/slider/useSliderPointer.js.map +1 -1
- package/lib/module/stepper/StepperRoot.js +115 -0
- package/lib/module/stepper/StepperRoot.js.map +1 -0
- package/lib/module/stepper/StepperStep.js +74 -0
- package/lib/module/stepper/StepperStep.js.map +1 -0
- package/lib/module/stepper/StepperStepPressable.js +60 -0
- package/lib/module/stepper/StepperStepPressable.js.map +1 -0
- package/lib/module/stepper/StepperStepTrigger.js +40 -0
- package/lib/module/stepper/StepperStepTrigger.js.map +1 -0
- package/lib/module/stepper/StepperStepTrigger.web.js +67 -0
- package/lib/module/stepper/StepperStepTrigger.web.js.map +1 -0
- package/lib/module/stepper/context.js +11 -0
- package/lib/module/stepper/context.js.map +1 -0
- package/lib/module/stepper/index.js +7 -0
- package/lib/module/stepper/index.js.map +1 -0
- package/lib/module/stepper/types.js +4 -0
- package/lib/module/stepper/types.js.map +1 -0
- package/lib/typescript/index.d.ts +1 -0
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/select/SelectEmpty.d.ts.map +1 -1
- package/lib/typescript/slider/useSliderPointer.d.ts.map +1 -1
- package/lib/typescript/stepper/StepperRoot.d.ts +7 -0
- package/lib/typescript/stepper/StepperRoot.d.ts.map +1 -0
- package/lib/typescript/stepper/StepperStep.d.ts +7 -0
- package/lib/typescript/stepper/StepperStep.d.ts.map +1 -0
- package/lib/typescript/stepper/StepperStepPressable.d.ts +8 -0
- package/lib/typescript/stepper/StepperStepPressable.d.ts.map +1 -0
- package/lib/typescript/stepper/StepperStepTrigger.d.ts +9 -0
- package/lib/typescript/stepper/StepperStepTrigger.d.ts.map +1 -0
- package/lib/typescript/stepper/StepperStepTrigger.web.d.ts +9 -0
- package/lib/typescript/stepper/StepperStepTrigger.web.d.ts.map +1 -0
- package/lib/typescript/stepper/context.d.ts +16 -0
- package/lib/typescript/stepper/context.d.ts.map +1 -0
- package/lib/typescript/stepper/index.d.ts +6 -0
- package/lib/typescript/stepper/index.d.ts.map +1 -0
- package/lib/typescript/stepper/types.d.ts +96 -0
- package/lib/typescript/stepper/types.d.ts.map +1 -0
- package/package.json +3 -3
- package/src/index.ts +1 -0
- package/src/select/SelectEmpty.tsx +2 -22
- package/src/slider/useSliderPointer.ts +9 -4
- package/src/stepper/StepperRoot.tsx +148 -0
- package/src/stepper/StepperStep.tsx +101 -0
- package/src/stepper/StepperStepPressable.tsx +48 -0
- package/src/stepper/StepperStepTrigger.tsx +38 -0
- package/src/stepper/StepperStepTrigger.web.tsx +69 -0
- package/src/stepper/context.tsx +11 -0
- package/src/stepper/index.ts +16 -0
- package/src/stepper/types.ts +104 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { forwardRef, type ComponentRef } from 'react';
|
|
2
|
+
import { View } from 'react-native';
|
|
3
|
+
import { dataAttributes, mergeDataAttributes } from '../utils/dataAttributes';
|
|
4
|
+
import { useStepperContext, useStepperStep } from './context';
|
|
5
|
+
import { StepperStepPressable } from './StepperStepPressable';
|
|
6
|
+
import type { IStepperStepTriggerProps } from './types';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Internal step region that carries the step's accessible name: the grouped visible
|
|
10
|
+
* content (indicator, title, description) followed by the state phrase. It becomes a
|
|
11
|
+
* `button` while the step is clickable. Not a public styled part; the styled
|
|
12
|
+
* `Stepper.Step` renders it around those slots.
|
|
13
|
+
*/
|
|
14
|
+
export const StepperStepTrigger = forwardRef<ComponentRef<typeof View>, IStepperStepTriggerProps>(
|
|
15
|
+
({ children, ...props }, ref) => {
|
|
16
|
+
const state = useStepperStep();
|
|
17
|
+
const { getStepAccessibilityText } = useStepperContext();
|
|
18
|
+
const phrase = getStepAccessibilityText(state);
|
|
19
|
+
const Comp = state.isClickable ? StepperStepPressable : View;
|
|
20
|
+
|
|
21
|
+
return (
|
|
22
|
+
<Comp
|
|
23
|
+
ref={ref}
|
|
24
|
+
accessible
|
|
25
|
+
accessibilityValue={phrase ? { text: phrase } : undefined}
|
|
26
|
+
{...props}
|
|
27
|
+
{...mergeDataAttributes(
|
|
28
|
+
props,
|
|
29
|
+
dataAttributes({ clickable: state.isClickable, disabled: state.isDisabled }),
|
|
30
|
+
)}
|
|
31
|
+
>
|
|
32
|
+
{children}
|
|
33
|
+
</Comp>
|
|
34
|
+
);
|
|
35
|
+
},
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
StepperStepTrigger.displayName = 'StepperStepTrigger';
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { forwardRef, useLayoutEffect, useMemo, useRef, type ComponentRef } from 'react';
|
|
2
|
+
import { View } from 'react-native';
|
|
3
|
+
import { VisuallyHidden } from '@react-aria/visually-hidden';
|
|
4
|
+
import { composeEventHandlers, mergeRefs } from '@cdx-ui/utils';
|
|
5
|
+
import { dataAttributes, mergeDataAttributes } from '../utils/dataAttributes';
|
|
6
|
+
import { useStepperContext, useStepperStep } from './context';
|
|
7
|
+
import { StepperStepPressable } from './StepperStepPressable';
|
|
8
|
+
import type { IStepperStepTriggerProps } from './types';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Internal step region that carries the step's accessible name. On web the state phrase
|
|
12
|
+
* is screen-reader-only text after the visible content, because browse-mode screen
|
|
13
|
+
* readers ignore `aria-label` on a list item. It becomes a `button` while the step is
|
|
14
|
+
* clickable. Not a public styled part.
|
|
15
|
+
*/
|
|
16
|
+
export const StepperStepTrigger = forwardRef<ComponentRef<typeof View>, IStepperStepTriggerProps>(
|
|
17
|
+
({ children, ...props }, ref) => {
|
|
18
|
+
const state = useStepperStep();
|
|
19
|
+
const { getStepAccessibilityText } = useStepperContext();
|
|
20
|
+
const phrase = getStepAccessibilityText(state);
|
|
21
|
+
const Comp = state.isClickable ? StepperStepPressable : View;
|
|
22
|
+
|
|
23
|
+
const nodeRef = useRef<ComponentRef<typeof View> | null>(null);
|
|
24
|
+
const mergedRef = useMemo(() => mergeRefs(ref, nodeRef), [ref]);
|
|
25
|
+
// Activating a step makes it current, which swaps the focused button for a plain region
|
|
26
|
+
// and would drop focus to the document. Focus moves to that region instead, outside the
|
|
27
|
+
// Tab order.
|
|
28
|
+
const restoreFocusRef = useRef(false);
|
|
29
|
+
|
|
30
|
+
useLayoutEffect(() => {
|
|
31
|
+
const node = nodeRef.current as unknown as HTMLElement | null;
|
|
32
|
+
if (!restoreFocusRef.current || state.isClickable || node === null) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
restoreFocusRef.current = false;
|
|
36
|
+
node.tabIndex = -1;
|
|
37
|
+
node.focus({ preventScroll: true });
|
|
38
|
+
}, [state.isClickable]);
|
|
39
|
+
|
|
40
|
+
const pressProps = state.isClickable
|
|
41
|
+
? {
|
|
42
|
+
onPress: () => {
|
|
43
|
+
restoreFocusRef.current =
|
|
44
|
+
document.activeElement === (nodeRef.current as unknown as HTMLElement | null);
|
|
45
|
+
},
|
|
46
|
+
onBlur: composeEventHandlers(props.onBlur, () => {
|
|
47
|
+
restoreFocusRef.current = false;
|
|
48
|
+
}),
|
|
49
|
+
}
|
|
50
|
+
: {};
|
|
51
|
+
|
|
52
|
+
return (
|
|
53
|
+
<Comp
|
|
54
|
+
ref={mergedRef}
|
|
55
|
+
{...props}
|
|
56
|
+
{...pressProps}
|
|
57
|
+
{...mergeDataAttributes(
|
|
58
|
+
props,
|
|
59
|
+
dataAttributes({ clickable: state.isClickable, disabled: state.isDisabled }),
|
|
60
|
+
)}
|
|
61
|
+
>
|
|
62
|
+
{children}
|
|
63
|
+
{phrase ? <VisuallyHidden elementType="span">{`, ${phrase}`}</VisuallyHidden> : null}
|
|
64
|
+
</Comp>
|
|
65
|
+
);
|
|
66
|
+
},
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
StepperStepTrigger.displayName = 'StepperStepTrigger';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createContext } from '@cdx-ui/utils';
|
|
2
|
+
import type { StepperContextValue, StepperStepState } from './types';
|
|
3
|
+
|
|
4
|
+
export const [StepperProvider, useStepperContext] =
|
|
5
|
+
createContext<StepperContextValue>('StepperContext');
|
|
6
|
+
|
|
7
|
+
/** Position of a direct child of `StepperRoot`, supplied by the root while it renders. */
|
|
8
|
+
export const [StepperIndexProvider, useStepperIndex] = createContext<number>('StepperIndex');
|
|
9
|
+
|
|
10
|
+
/** Resolved state of the enclosing step. `useStepperStep` throws outside a `StepperStep`. */
|
|
11
|
+
export const [StepperStepProvider, useStepperStep] = createContext<StepperStepState>('StepperStep');
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
IStepperProps,
|
|
3
|
+
IStepperStepProps,
|
|
4
|
+
IStepperStepTriggerProps,
|
|
5
|
+
InterfaceStepperProps,
|
|
6
|
+
InterfaceStepperStepProps,
|
|
7
|
+
StepperContextValue,
|
|
8
|
+
StepperLabelPlacement,
|
|
9
|
+
StepperOrientation,
|
|
10
|
+
StepperStepState,
|
|
11
|
+
} from './types';
|
|
12
|
+
|
|
13
|
+
export { useStepperContext, useStepperIndex, useStepperStep } from './context';
|
|
14
|
+
export { StepperRoot, type StepperRootProps } from './StepperRoot';
|
|
15
|
+
export { StepperStep, type StepperStepProps } from './StepperStep';
|
|
16
|
+
export { StepperStepTrigger } from './StepperStepTrigger';
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import type { ViewProps } from 'react-native';
|
|
2
|
+
|
|
3
|
+
export type StepperOrientation = 'horizontal' | 'vertical';
|
|
4
|
+
|
|
5
|
+
export type StepperLabelPlacement = 'end' | 'bottom';
|
|
6
|
+
|
|
7
|
+
export interface StepperStepState {
|
|
8
|
+
/** Zero-based position among the steps. */
|
|
9
|
+
readonly index: number;
|
|
10
|
+
/** Number of steps rendered. */
|
|
11
|
+
readonly count: number;
|
|
12
|
+
readonly isCurrent: boolean;
|
|
13
|
+
/** Resolved completion: the explicit `isComplete`, or `index < currentStep`. */
|
|
14
|
+
readonly isComplete: boolean;
|
|
15
|
+
/** Clamped to 0–100. Non-finite values are 0. */
|
|
16
|
+
readonly progress: number;
|
|
17
|
+
/** True when the partial ring is drawn: `0 < progress` and not complete. */
|
|
18
|
+
readonly hasProgress: boolean;
|
|
19
|
+
readonly isLast: boolean;
|
|
20
|
+
/** True when the step renders as a button. */
|
|
21
|
+
readonly isClickable: boolean;
|
|
22
|
+
readonly isDisabled: boolean;
|
|
23
|
+
readonly orientation: StepperOrientation;
|
|
24
|
+
readonly labelPlacement: StepperLabelPlacement;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface InterfaceStepperProps extends ViewProps {
|
|
28
|
+
/**
|
|
29
|
+
* Controlled, zero-based current step. A value outside `0…count-1` means no step is
|
|
30
|
+
* current; a value `>= count` makes every step complete by default.
|
|
31
|
+
*/
|
|
32
|
+
readonly currentStep?: number;
|
|
33
|
+
/**
|
|
34
|
+
* Uncontrolled starting step. Ignored when `currentStep` is set.
|
|
35
|
+
* @default 0
|
|
36
|
+
*/
|
|
37
|
+
readonly defaultStep?: number;
|
|
38
|
+
/** Called with the pressed step's index. Setting it makes eligible steps clickable. */
|
|
39
|
+
readonly onStepChange?: (index: number) => void;
|
|
40
|
+
/**
|
|
41
|
+
* When `true`, only complete steps are clickable. When `false`, every non-current,
|
|
42
|
+
* non-disabled step is.
|
|
43
|
+
* @default true
|
|
44
|
+
*/
|
|
45
|
+
readonly isLinear?: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* `'horizontal'` lays the steps out in a row. `labelPlacement` chooses where each label sits.
|
|
48
|
+
* @default 'vertical'
|
|
49
|
+
*/
|
|
50
|
+
readonly orientation?: StepperOrientation;
|
|
51
|
+
/**
|
|
52
|
+
* Horizontal only; ignored when `orientation` is `'vertical'`. `'end'` puts each label after
|
|
53
|
+
* its indicator, sizes steps to their labels and stretches the separators between them.
|
|
54
|
+
* `'bottom'` gives every step an equal column with the indicator and label centered.
|
|
55
|
+
* @default 'end'
|
|
56
|
+
*/
|
|
57
|
+
readonly labelPlacement?: StepperLabelPlacement;
|
|
58
|
+
/**
|
|
59
|
+
* Returns the state phrase appended to each step's title for assistive technology,
|
|
60
|
+
* replacing the English default (for example `"current step, 40% complete"`).
|
|
61
|
+
*/
|
|
62
|
+
readonly getStepAccessibilityText?: (state: StepperStepState) => string;
|
|
63
|
+
/** @default false */
|
|
64
|
+
readonly asChild?: boolean;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export type IStepperProps = InterfaceStepperProps;
|
|
68
|
+
|
|
69
|
+
export interface InterfaceStepperStepProps extends ViewProps {
|
|
70
|
+
/**
|
|
71
|
+
* Whether the step is complete. An explicit value wins in both directions.
|
|
72
|
+
* @default index < currentStep
|
|
73
|
+
*/
|
|
74
|
+
readonly isComplete?: boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Partial progress, 0–100. Values outside the range are clamped; non-finite values are `0`.
|
|
77
|
+
* @default 0
|
|
78
|
+
*/
|
|
79
|
+
readonly progress?: number;
|
|
80
|
+
/**
|
|
81
|
+
* Only affects clickability. A disabled step still shows its state.
|
|
82
|
+
* @default false
|
|
83
|
+
*/
|
|
84
|
+
readonly isDisabled?: boolean;
|
|
85
|
+
/** @default false */
|
|
86
|
+
readonly asChild?: boolean;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export type IStepperStepProps = InterfaceStepperStepProps;
|
|
90
|
+
|
|
91
|
+
export type IStepperStepTriggerProps = ViewProps;
|
|
92
|
+
|
|
93
|
+
export interface StepperContextValue {
|
|
94
|
+
/** Resolved current step (controlled or uncontrolled). */
|
|
95
|
+
readonly currentStep: number;
|
|
96
|
+
readonly count: number;
|
|
97
|
+
readonly orientation: StepperOrientation;
|
|
98
|
+
readonly labelPlacement: StepperLabelPlacement;
|
|
99
|
+
readonly isLinear: boolean;
|
|
100
|
+
/** True when the consumer passed `onStepChange`. */
|
|
101
|
+
readonly hasStepChangeHandler: boolean;
|
|
102
|
+
readonly setCurrentStep: (index: number) => void;
|
|
103
|
+
readonly getStepAccessibilityText: (state: StepperStepState) => string;
|
|
104
|
+
}
|