@cdx-ui/primitives 0.0.1-alpha.1 → 0.0.1-alpha.10

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.
Files changed (122) hide show
  1. package/lib/commonjs/button/createButtonRoot.js +3 -3
  2. package/lib/commonjs/button/createButtonRoot.js.map +1 -1
  3. package/lib/commonjs/checkbox/context.js +14 -0
  4. package/lib/commonjs/checkbox/context.js.map +1 -0
  5. package/lib/commonjs/checkbox/createCheckboxGroup.js +44 -0
  6. package/lib/commonjs/checkbox/createCheckboxGroup.js.map +1 -0
  7. package/lib/commonjs/checkbox/createCheckboxIcon.js +28 -0
  8. package/lib/commonjs/checkbox/createCheckboxIcon.js.map +1 -0
  9. package/lib/commonjs/checkbox/createCheckboxIndicator.js +47 -0
  10. package/lib/commonjs/checkbox/createCheckboxIndicator.js.map +1 -0
  11. package/lib/commonjs/checkbox/createCheckboxLabel.js +46 -0
  12. package/lib/commonjs/checkbox/createCheckboxLabel.js.map +1 -0
  13. package/lib/commonjs/checkbox/createCheckboxRoot.js +99 -0
  14. package/lib/commonjs/checkbox/createCheckboxRoot.js.map +1 -0
  15. package/lib/commonjs/checkbox/createCheckboxRoot.web.js +88 -0
  16. package/lib/commonjs/checkbox/createCheckboxRoot.web.js.map +1 -0
  17. package/lib/commonjs/checkbox/index.js +32 -0
  18. package/lib/commonjs/checkbox/index.js.map +1 -0
  19. package/lib/commonjs/checkbox/types.js +6 -0
  20. package/lib/commonjs/checkbox/types.js.map +1 -0
  21. package/lib/commonjs/checkbox/useCheckboxRoot.js +82 -0
  22. package/lib/commonjs/checkbox/useCheckboxRoot.js.map +1 -0
  23. package/lib/commonjs/index.js +25 -1
  24. package/lib/commonjs/index.js.map +1 -1
  25. package/lib/commonjs/switch/createSwitchRoot.js +73 -0
  26. package/lib/commonjs/switch/createSwitchRoot.js.map +1 -0
  27. package/lib/commonjs/switch/index.js +15 -0
  28. package/lib/commonjs/switch/index.js.map +1 -0
  29. package/lib/commonjs/switch/types.js +6 -0
  30. package/lib/commonjs/switch/types.js.map +1 -0
  31. package/lib/commonjs/types.js +2 -0
  32. package/lib/commonjs/types.js.map +1 -0
  33. package/lib/module/button/createButtonRoot.js +3 -3
  34. package/lib/module/button/createButtonRoot.js.map +1 -1
  35. package/lib/module/checkbox/context.js +7 -0
  36. package/lib/module/checkbox/context.js.map +1 -0
  37. package/lib/module/checkbox/createCheckboxGroup.js +39 -0
  38. package/lib/module/checkbox/createCheckboxGroup.js.map +1 -0
  39. package/lib/module/checkbox/createCheckboxIcon.js +23 -0
  40. package/lib/module/checkbox/createCheckboxIcon.js.map +1 -0
  41. package/lib/module/checkbox/createCheckboxIndicator.js +42 -0
  42. package/lib/module/checkbox/createCheckboxIndicator.js.map +1 -0
  43. package/lib/module/checkbox/createCheckboxLabel.js +41 -0
  44. package/lib/module/checkbox/createCheckboxLabel.js.map +1 -0
  45. package/lib/module/checkbox/createCheckboxRoot.js +94 -0
  46. package/lib/module/checkbox/createCheckboxRoot.js.map +1 -0
  47. package/lib/module/checkbox/createCheckboxRoot.web.js +83 -0
  48. package/lib/module/checkbox/createCheckboxRoot.web.js.map +1 -0
  49. package/lib/module/checkbox/index.js +27 -0
  50. package/lib/module/checkbox/index.js.map +1 -0
  51. package/lib/module/checkbox/types.js +4 -0
  52. package/lib/module/checkbox/types.js.map +1 -0
  53. package/lib/module/checkbox/useCheckboxRoot.js +78 -0
  54. package/lib/module/checkbox/useCheckboxRoot.js.map +1 -0
  55. package/lib/module/index.js +3 -1
  56. package/lib/module/index.js.map +1 -1
  57. package/lib/module/switch/createSwitchRoot.js +67 -0
  58. package/lib/module/switch/createSwitchRoot.js.map +1 -0
  59. package/lib/module/switch/index.js +11 -0
  60. package/lib/module/switch/index.js.map +1 -0
  61. package/lib/module/switch/types.js +4 -0
  62. package/lib/module/switch/types.js.map +1 -0
  63. package/lib/module/types.js +2 -0
  64. package/lib/module/types.js.map +1 -0
  65. package/lib/typescript/button/context.d.ts +1 -1
  66. package/lib/typescript/button/context.d.ts.map +1 -1
  67. package/lib/typescript/button/createButtonRoot.d.ts +1 -1
  68. package/lib/typescript/button/createButtonRoot.d.ts.map +1 -1
  69. package/lib/typescript/button/types.d.ts +0 -7
  70. package/lib/typescript/button/types.d.ts.map +1 -1
  71. package/lib/typescript/checkbox/context.d.ts +18 -0
  72. package/lib/typescript/checkbox/context.d.ts.map +1 -0
  73. package/lib/typescript/checkbox/createCheckboxGroup.d.ts +3 -0
  74. package/lib/typescript/checkbox/createCheckboxGroup.d.ts.map +1 -0
  75. package/lib/typescript/checkbox/createCheckboxIcon.d.ts +3 -0
  76. package/lib/typescript/checkbox/createCheckboxIcon.d.ts.map +1 -0
  77. package/lib/typescript/checkbox/createCheckboxIndicator.d.ts +5 -0
  78. package/lib/typescript/checkbox/createCheckboxIndicator.d.ts.map +1 -0
  79. package/lib/typescript/checkbox/createCheckboxLabel.d.ts +5 -0
  80. package/lib/typescript/checkbox/createCheckboxLabel.d.ts.map +1 -0
  81. package/lib/typescript/checkbox/createCheckboxRoot.d.ts +2 -0
  82. package/lib/typescript/checkbox/createCheckboxRoot.d.ts.map +1 -0
  83. package/lib/typescript/checkbox/createCheckboxRoot.web.d.ts +2 -0
  84. package/lib/typescript/checkbox/createCheckboxRoot.web.d.ts.map +1 -0
  85. package/lib/typescript/checkbox/index.d.ts +10 -0
  86. package/lib/typescript/checkbox/index.d.ts.map +1 -0
  87. package/lib/typescript/checkbox/types.d.ts +57 -0
  88. package/lib/typescript/checkbox/types.d.ts.map +1 -0
  89. package/lib/typescript/checkbox/useCheckboxRoot.d.ts +147 -0
  90. package/lib/typescript/checkbox/useCheckboxRoot.d.ts.map +1 -0
  91. package/lib/typescript/index.d.ts +4 -1
  92. package/lib/typescript/index.d.ts.map +1 -1
  93. package/lib/typescript/select/types.d.ts +2 -7
  94. package/lib/typescript/select/types.d.ts.map +1 -1
  95. package/lib/typescript/switch/createSwitchRoot.d.ts +3 -0
  96. package/lib/typescript/switch/createSwitchRoot.d.ts.map +1 -0
  97. package/lib/typescript/switch/index.d.ts +7 -0
  98. package/lib/typescript/switch/index.d.ts.map +1 -0
  99. package/lib/typescript/switch/types.d.ts +38 -0
  100. package/lib/typescript/switch/types.d.ts.map +1 -0
  101. package/lib/typescript/types.d.ts +8 -0
  102. package/lib/typescript/types.d.ts.map +1 -0
  103. package/package.json +12 -9
  104. package/src/button/context.tsx +1 -1
  105. package/src/button/createButtonRoot.tsx +5 -4
  106. package/src/button/types.ts +0 -8
  107. package/src/checkbox/context.tsx +18 -0
  108. package/src/checkbox/createCheckboxGroup.tsx +32 -0
  109. package/src/checkbox/createCheckboxIcon.tsx +18 -0
  110. package/src/checkbox/createCheckboxIndicator.tsx +43 -0
  111. package/src/checkbox/createCheckboxLabel.tsx +42 -0
  112. package/src/checkbox/createCheckboxRoot.tsx +100 -0
  113. package/src/checkbox/createCheckboxRoot.web.tsx +77 -0
  114. package/src/checkbox/index.ts +43 -0
  115. package/src/checkbox/types.ts +74 -0
  116. package/src/checkbox/useCheckboxRoot.ts +85 -0
  117. package/src/index.ts +4 -1
  118. package/src/select/types.ts +2 -10
  119. package/src/switch/createSwitchRoot.tsx +72 -0
  120. package/src/switch/index.ts +16 -0
  121. package/src/switch/types.ts +42 -0
  122. package/src/types.ts +7 -0
@@ -0,0 +1,85 @@
1
+ import { useContext, useRef } from 'react';
2
+ import { mergeRefs, useFormControlContext } from '@cdx-ui/utils';
3
+ import { useCheckbox } from '@react-native-aria/checkbox';
4
+ import { useHover } from '@react-native-aria/interactions';
5
+ import { useToggleState } from '@react-stately/toggle';
6
+ import { CheckboxGroupContext } from './context';
7
+ import type { ICheckboxProps } from './types';
8
+
9
+ interface UseCheckboxRootOptions {
10
+ useInputRefForAria?: boolean;
11
+ }
12
+
13
+ export function useCheckboxRoot(
14
+ props: ICheckboxProps,
15
+ ref?: React.Ref<unknown>,
16
+ { useInputRefForAria = false }: UseCheckboxRootOptions = {},
17
+ ) {
18
+ const formControlContext = useFormControlContext();
19
+
20
+ const { isInvalid, isReadOnly, isIndeterminate, ...combinedProps } = {
21
+ ...formControlContext,
22
+ ...props,
23
+ };
24
+
25
+ const checkboxGroupContext = useContext(CheckboxGroupContext);
26
+
27
+ const state = useToggleState({
28
+ ...combinedProps,
29
+ defaultSelected: props.defaultIsChecked,
30
+ isSelected: props.isChecked,
31
+ });
32
+
33
+ const rootRef = useRef(null);
34
+ const inputRef = useRef<HTMLInputElement | null>(null);
35
+ const mergedRootRef = mergeRefs(ref as any, rootRef as any);
36
+ const ariaLabel = combinedProps['aria-label'] || combinedProps.value || 'Checkbox';
37
+
38
+ const groupToggleState = useToggleState(
39
+ checkboxGroupContext
40
+ ? {
41
+ isReadOnly: isReadOnly || checkboxGroupContext.state.isReadOnly,
42
+ isSelected: checkboxGroupContext.state.isSelected(combinedProps.value),
43
+ onChange(isSelected: boolean) {
44
+ if (isSelected) {
45
+ checkboxGroupContext.state.addValue(combinedProps.value);
46
+ } else {
47
+ checkboxGroupContext.state.removeValue(combinedProps.value);
48
+ }
49
+ combinedProps.onChange?.(isSelected);
50
+ },
51
+ }
52
+ : { isSelected: false },
53
+ );
54
+
55
+ const { inputProps: groupItemInputProps } = useCheckbox(
56
+ {
57
+ ...combinedProps,
58
+ 'aria-label': ariaLabel,
59
+ ...(checkboxGroupContext && {
60
+ isReadOnly: isReadOnly || checkboxGroupContext.state.isReadOnly,
61
+ isDisabled: combinedProps.isDisabled || checkboxGroupContext.state.isDisabled,
62
+ }),
63
+ } as any,
64
+ checkboxGroupContext ? groupToggleState : state,
65
+ (useInputRefForAria ? inputRef : rootRef) as any,
66
+ );
67
+
68
+ const { checked: isChecked, disabled: isDisabled } = groupItemInputProps;
69
+
70
+ const { hoverProps, isHovered } = useHover({}, rootRef);
71
+
72
+ return {
73
+ combinedProps,
74
+ isInvalid,
75
+ isReadOnly,
76
+ isIndeterminate,
77
+ groupItemInputProps,
78
+ isChecked,
79
+ isDisabled,
80
+ isHovered,
81
+ hoverProps,
82
+ mergedRef: mergedRootRef,
83
+ inputRef,
84
+ };
85
+ }
package/src/index.ts CHANGED
@@ -1,4 +1,7 @@
1
1
  export * from './button';
2
+ export * from './checkbox';
2
3
  export * from './input';
4
+ export { type EdgeInsets, OverlayInsetsProvider } from './overlay';
3
5
  export * from './select';
4
- export { OverlayInsetsProvider, type EdgeInsets } from './overlay';
6
+ export * from './switch';
7
+ export type { InteractionState } from './types';
@@ -1,16 +1,8 @@
1
1
  import type { PropsWithoutRef, ReactNode, RefAttributes, RefObject } from 'react';
2
2
  import type { PressableProps, ViewProps } from 'react-native';
3
3
  import type { EntryOrExitLayoutType } from 'react-native-reanimated';
4
-
5
- // TODO: Consolidate with button/types.ts
6
-
7
- export interface InteractionState {
8
- hover: boolean;
9
- focus: boolean;
10
- active: boolean;
11
- disabled: boolean;
12
- focusVisible: boolean;
13
- }
4
+ import type { InteractionState } from '../types';
5
+ export type { InteractionState };
14
6
 
15
7
  export interface SelectItemInteractionState extends InteractionState {
16
8
  highlighted: boolean;
@@ -0,0 +1,72 @@
1
+ import React, { forwardRef } from 'react';
2
+ import { useFocusRing } from '@react-native-aria/focus';
3
+ import { useHover } from '@react-native-aria/interactions';
4
+ import { useToggleState } from '@react-stately/toggle';
5
+ import { useFormControlContext, mergeRefs } from '@cdx-ui/utils';
6
+ import type { ISwitchProps } from './types';
7
+
8
+ type MergeableRef = Parameters<typeof mergeRefs>[0];
9
+
10
+ export const createSwitchRoot = <T, R = unknown>(BaseSwitch: React.ComponentType<T>) =>
11
+ forwardRef<R, ISwitchProps>(
12
+ (
13
+ {
14
+ disabled,
15
+ isDisabled,
16
+ isChecked,
17
+ isInvalid,
18
+ defaultValue,
19
+ value,
20
+ onValueChange,
21
+ ...props
22
+ }: ISwitchProps,
23
+ ref: React.ForwardedRef<R>,
24
+ ) => {
25
+ const formControlContext = useFormControlContext();
26
+ const combinedProps = { ...formControlContext, ...props };
27
+ const controlledValue = value ?? isChecked;
28
+ const state = useToggleState({
29
+ defaultSelected: defaultValue ?? false,
30
+ isSelected: controlledValue,
31
+ onChange: onValueChange,
32
+ });
33
+
34
+ const checked = controlledValue ?? state.isSelected;
35
+
36
+ const _ref = React.useRef(null);
37
+ const { isHovered } = useHover({}, _ref);
38
+ const { isFocusVisible, focusProps } = useFocusRing();
39
+
40
+ const mergedRef = mergeRefs(ref as MergeableRef, _ref);
41
+
42
+ const isDisabledResolved = disabled || isDisabled || combinedProps.isDisabled;
43
+ const isInvalidResolved = isInvalid || combinedProps.isInvalid;
44
+
45
+ return (
46
+ <BaseSwitch
47
+ states={{
48
+ hover: isHovered,
49
+ disabled: isDisabledResolved,
50
+ invalid: isInvalidResolved,
51
+ checked,
52
+ focusVisible: isFocusVisible,
53
+ }}
54
+ dataSet={{
55
+ hover: isHovered ? 'true' : 'false',
56
+ disabled: isDisabledResolved ? 'true' : 'false',
57
+ invalid: isInvalidResolved ? 'true' : 'false',
58
+ checked: checked ? 'true' : 'false',
59
+ 'focus-visible': isFocusVisible ? 'true' : 'false',
60
+ }}
61
+ disabled={isDisabledResolved}
62
+ {...focusProps}
63
+ onValueChange={(val: boolean) => {
64
+ state.setSelected(val);
65
+ }}
66
+ value={checked}
67
+ {...(combinedProps as T)}
68
+ ref={mergedRef as React.Ref<R>}
69
+ />
70
+ );
71
+ },
72
+ );
@@ -0,0 +1,16 @@
1
+ import type React from 'react';
2
+ import { createSwitchRoot } from './createSwitchRoot';
3
+ import type { ISwitchComponentType } from './types';
4
+
5
+ export type { ISwitchProps, ISwitchComponentType, IInterfaceSwitchProps } from './types';
6
+
7
+ export function createSwitch<T, R = unknown>({
8
+ Root,
9
+ }: {
10
+ Root: React.ComponentType<T>;
11
+ }): ISwitchComponentType<T, R> {
12
+ const Switch = createSwitchRoot(Root);
13
+
14
+ Switch.displayName = 'Switch';
15
+ return Switch as ISwitchComponentType<T, R>;
16
+ }
@@ -0,0 +1,42 @@
1
+ import type React from 'react';
2
+
3
+ export interface IInterfaceSwitchProps {
4
+ /**
5
+ * Callback invoked when the switch value changes.
6
+ */
7
+ onValueChange?: (value: boolean) => void;
8
+ /**
9
+ * If true, set the Switch to the checked state.
10
+ */
11
+ isChecked?: boolean;
12
+ /**
13
+ * The current value of the switch.
14
+ */
15
+ value?: boolean;
16
+ /**
17
+ * If true, the switch will be disabled.
18
+ */
19
+ isDisabled?: boolean;
20
+ /**
21
+ * If true, the switch will be disabled.
22
+ */
23
+ disabled?: boolean;
24
+ /**
25
+ * If true, the checkbox will be initially checked.
26
+ */
27
+ defaultValue?: boolean;
28
+ /**
29
+ * If true, set the switch to the invalid state.
30
+ */
31
+ isInvalid?: boolean;
32
+ /**
33
+ * If true, the switch will be hovered.
34
+ */
35
+ isHovered?: boolean;
36
+ }
37
+
38
+ export type ISwitchComponentType<T, R = unknown> = React.ForwardRefExoticComponent<
39
+ React.PropsWithoutRef<T & IInterfaceSwitchProps> & React.RefAttributes<R>
40
+ >;
41
+
42
+ export type ISwitchProps = IInterfaceSwitchProps;
package/src/types.ts ADDED
@@ -0,0 +1,7 @@
1
+ export interface InteractionState {
2
+ hover: boolean;
3
+ focus: boolean;
4
+ active: boolean;
5
+ disabled: boolean;
6
+ focusVisible: boolean;
7
+ }