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

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 (147) 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/button/index.js +18 -19
  4. package/lib/commonjs/button/index.js.map +1 -1
  5. package/lib/commonjs/checkbox/context.js +14 -0
  6. package/lib/commonjs/checkbox/context.js.map +1 -0
  7. package/lib/commonjs/checkbox/createCheckboxGroup.js +44 -0
  8. package/lib/commonjs/checkbox/createCheckboxGroup.js.map +1 -0
  9. package/lib/commonjs/checkbox/createCheckboxIcon.js +28 -0
  10. package/lib/commonjs/checkbox/createCheckboxIcon.js.map +1 -0
  11. package/lib/commonjs/checkbox/createCheckboxIndicator.js +47 -0
  12. package/lib/commonjs/checkbox/createCheckboxIndicator.js.map +1 -0
  13. package/lib/commonjs/checkbox/createCheckboxLabel.js +46 -0
  14. package/lib/commonjs/checkbox/createCheckboxLabel.js.map +1 -0
  15. package/lib/commonjs/checkbox/createCheckboxRoot.js +99 -0
  16. package/lib/commonjs/checkbox/createCheckboxRoot.js.map +1 -0
  17. package/lib/commonjs/checkbox/createCheckboxRoot.web.js +90 -0
  18. package/lib/commonjs/checkbox/createCheckboxRoot.web.js.map +1 -0
  19. package/lib/commonjs/checkbox/index.js +30 -0
  20. package/lib/commonjs/checkbox/index.js.map +1 -0
  21. package/lib/commonjs/checkbox/types.js +6 -0
  22. package/lib/commonjs/checkbox/types.js.map +1 -0
  23. package/lib/commonjs/checkbox/useCheckboxRoot.js +82 -0
  24. package/lib/commonjs/checkbox/useCheckboxRoot.js.map +1 -0
  25. package/lib/commonjs/index.js +25 -1
  26. package/lib/commonjs/index.js.map +1 -1
  27. package/lib/commonjs/input/index.js +16 -18
  28. package/lib/commonjs/input/index.js.map +1 -1
  29. package/lib/commonjs/select/context.js.map +1 -1
  30. package/lib/commonjs/switch/createSwitchRoot.js +73 -0
  31. package/lib/commonjs/switch/createSwitchRoot.js.map +1 -0
  32. package/lib/commonjs/switch/index.js +15 -0
  33. package/lib/commonjs/switch/index.js.map +1 -0
  34. package/lib/commonjs/switch/types.js +6 -0
  35. package/lib/commonjs/switch/types.js.map +1 -0
  36. package/lib/commonjs/types.js +2 -0
  37. package/lib/commonjs/types.js.map +1 -0
  38. package/lib/commonjs/utils/dataAttributes.web.js +1 -4
  39. package/lib/commonjs/utils/dataAttributes.web.js.map +1 -1
  40. package/lib/module/button/createButtonRoot.js +3 -3
  41. package/lib/module/button/createButtonRoot.js.map +1 -1
  42. package/lib/module/button/index.js +18 -19
  43. package/lib/module/button/index.js.map +1 -1
  44. package/lib/module/checkbox/context.js +7 -0
  45. package/lib/module/checkbox/context.js.map +1 -0
  46. package/lib/module/checkbox/createCheckboxGroup.js +39 -0
  47. package/lib/module/checkbox/createCheckboxGroup.js.map +1 -0
  48. package/lib/module/checkbox/createCheckboxIcon.js +23 -0
  49. package/lib/module/checkbox/createCheckboxIcon.js.map +1 -0
  50. package/lib/module/checkbox/createCheckboxIndicator.js +42 -0
  51. package/lib/module/checkbox/createCheckboxIndicator.js.map +1 -0
  52. package/lib/module/checkbox/createCheckboxLabel.js +41 -0
  53. package/lib/module/checkbox/createCheckboxLabel.js.map +1 -0
  54. package/lib/module/checkbox/createCheckboxRoot.js +94 -0
  55. package/lib/module/checkbox/createCheckboxRoot.js.map +1 -0
  56. package/lib/module/checkbox/createCheckboxRoot.web.js +85 -0
  57. package/lib/module/checkbox/createCheckboxRoot.web.js.map +1 -0
  58. package/lib/module/checkbox/index.js +26 -0
  59. package/lib/module/checkbox/index.js.map +1 -0
  60. package/lib/module/checkbox/types.js +4 -0
  61. package/lib/module/checkbox/types.js.map +1 -0
  62. package/lib/module/checkbox/useCheckboxRoot.js +78 -0
  63. package/lib/module/checkbox/useCheckboxRoot.js.map +1 -0
  64. package/lib/module/index.js +3 -1
  65. package/lib/module/index.js.map +1 -1
  66. package/lib/module/input/index.js +15 -16
  67. package/lib/module/input/index.js.map +1 -1
  68. package/lib/module/select/context.js.map +1 -1
  69. package/lib/module/switch/createSwitchRoot.js +67 -0
  70. package/lib/module/switch/createSwitchRoot.js.map +1 -0
  71. package/lib/module/switch/index.js +11 -0
  72. package/lib/module/switch/index.js.map +1 -0
  73. package/lib/module/switch/types.js +4 -0
  74. package/lib/module/switch/types.js.map +1 -0
  75. package/lib/module/types.js +2 -0
  76. package/lib/module/types.js.map +1 -0
  77. package/lib/module/utils/dataAttributes.web.js +1 -4
  78. package/lib/module/utils/dataAttributes.web.js.map +1 -1
  79. package/lib/typescript/button/context.d.ts +1 -1
  80. package/lib/typescript/button/context.d.ts.map +1 -1
  81. package/lib/typescript/button/createButtonRoot.d.ts +1 -1
  82. package/lib/typescript/button/createButtonRoot.d.ts.map +1 -1
  83. package/lib/typescript/button/index.d.ts +1 -1
  84. package/lib/typescript/button/index.d.ts.map +1 -1
  85. package/lib/typescript/button/types.d.ts +0 -7
  86. package/lib/typescript/button/types.d.ts.map +1 -1
  87. package/lib/typescript/checkbox/context.d.ts +18 -0
  88. package/lib/typescript/checkbox/context.d.ts.map +1 -0
  89. package/lib/typescript/checkbox/createCheckboxGroup.d.ts +3 -0
  90. package/lib/typescript/checkbox/createCheckboxGroup.d.ts.map +1 -0
  91. package/lib/typescript/checkbox/createCheckboxIcon.d.ts +3 -0
  92. package/lib/typescript/checkbox/createCheckboxIcon.d.ts.map +1 -0
  93. package/lib/typescript/checkbox/createCheckboxIndicator.d.ts +5 -0
  94. package/lib/typescript/checkbox/createCheckboxIndicator.d.ts.map +1 -0
  95. package/lib/typescript/checkbox/createCheckboxLabel.d.ts +5 -0
  96. package/lib/typescript/checkbox/createCheckboxLabel.d.ts.map +1 -0
  97. package/lib/typescript/checkbox/createCheckboxRoot.d.ts +2 -0
  98. package/lib/typescript/checkbox/createCheckboxRoot.d.ts.map +1 -0
  99. package/lib/typescript/checkbox/createCheckboxRoot.web.d.ts +2 -0
  100. package/lib/typescript/checkbox/createCheckboxRoot.web.d.ts.map +1 -0
  101. package/lib/typescript/checkbox/index.d.ts +11 -0
  102. package/lib/typescript/checkbox/index.d.ts.map +1 -0
  103. package/lib/typescript/checkbox/types.d.ts +57 -0
  104. package/lib/typescript/checkbox/types.d.ts.map +1 -0
  105. package/lib/typescript/checkbox/useCheckboxRoot.d.ts +147 -0
  106. package/lib/typescript/checkbox/useCheckboxRoot.d.ts.map +1 -0
  107. package/lib/typescript/index.d.ts +4 -1
  108. package/lib/typescript/index.d.ts.map +1 -1
  109. package/lib/typescript/input/index.d.ts +3 -2
  110. package/lib/typescript/input/index.d.ts.map +1 -1
  111. package/lib/typescript/select/context.d.ts +2 -1
  112. package/lib/typescript/select/context.d.ts.map +1 -1
  113. package/lib/typescript/select/types.d.ts +1 -7
  114. package/lib/typescript/select/types.d.ts.map +1 -1
  115. package/lib/typescript/switch/createSwitchRoot.d.ts +3 -0
  116. package/lib/typescript/switch/createSwitchRoot.d.ts.map +1 -0
  117. package/lib/typescript/switch/index.d.ts +7 -0
  118. package/lib/typescript/switch/index.d.ts.map +1 -0
  119. package/lib/typescript/switch/types.d.ts +38 -0
  120. package/lib/typescript/switch/types.d.ts.map +1 -0
  121. package/lib/typescript/types.d.ts +8 -0
  122. package/lib/typescript/types.d.ts.map +1 -0
  123. package/lib/typescript/utils/dataAttributes.web.d.ts.map +1 -1
  124. package/package.json +12 -9
  125. package/src/button/context.tsx +1 -1
  126. package/src/button/createButtonRoot.tsx +5 -4
  127. package/src/button/index.tsx +18 -19
  128. package/src/button/types.ts +0 -8
  129. package/src/checkbox/context.tsx +18 -0
  130. package/src/checkbox/createCheckboxGroup.tsx +32 -0
  131. package/src/checkbox/createCheckboxIcon.tsx +18 -0
  132. package/src/checkbox/createCheckboxIndicator.tsx +43 -0
  133. package/src/checkbox/createCheckboxLabel.tsx +42 -0
  134. package/src/checkbox/createCheckboxRoot.tsx +100 -0
  135. package/src/checkbox/createCheckboxRoot.web.tsx +79 -0
  136. package/src/checkbox/index.ts +43 -0
  137. package/src/checkbox/types.ts +74 -0
  138. package/src/checkbox/useCheckboxRoot.ts +85 -0
  139. package/src/index.ts +4 -1
  140. package/src/input/index.tsx +18 -25
  141. package/src/select/context.tsx +2 -1
  142. package/src/select/types.ts +1 -10
  143. package/src/switch/createSwitchRoot.tsx +72 -0
  144. package/src/switch/index.ts +16 -0
  145. package/src/switch/types.ts +42 -0
  146. package/src/types.ts +7 -0
  147. package/src/utils/dataAttributes.web.ts +2 -4
@@ -1,3 +1,4 @@
1
+ import type React from 'react';
1
2
  import { createInputField } from './createInputField';
2
3
  import { createInputIcon } from './createInputIcon';
3
4
  import { createInputRoot } from './createInputRoot';
@@ -12,40 +13,32 @@ export type {
12
13
  IInputSlotProps,
13
14
  } from './types';
14
15
 
15
- export const createInput = <
16
+ export function createInput<
16
17
  RootProps,
17
18
  IconProps,
18
19
  SlotProps,
19
20
  FieldProps,
20
21
  RootRef = unknown,
21
22
  FieldRef = unknown,
22
- >({
23
- Root,
24
- Icon,
25
- Slot,
26
- Field,
27
- }: {
23
+ >(BaseComponents: {
28
24
  Root: React.ComponentType<RootProps>;
29
25
  Icon: React.ComponentType<IconProps>;
30
26
  Slot: React.ComponentType<SlotProps>;
31
27
  Field: React.ComponentType<FieldProps>;
32
- }) => {
33
- const Input = createInputRoot(Root) as any;
34
- Input.Icon = createInputIcon(Icon);
35
- Input.Slot = createInputSlot(Slot);
36
- Input.Field = createInputField(Field);
28
+ }) {
29
+ const Input = createInputRoot(BaseComponents.Root);
30
+ const Icon = createInputIcon(BaseComponents.Icon);
31
+ const Slot = createInputSlot(BaseComponents.Slot);
32
+ const Field = createInputField(BaseComponents.Field);
37
33
 
38
- Input.displayName = 'Input';
39
- Input.Icon.displayName = 'Input.Icon';
40
- Input.Slot.displayName = 'Input.Slot';
41
- Input.Field.displayName = 'Input.Field';
34
+ Input.displayName = 'InputPrimitive';
35
+ Icon.displayName = 'InputPrimitive.Icon';
36
+ Slot.displayName = 'InputPrimitive.Slot';
37
+ Field.displayName = 'InputPrimitive.Field';
42
38
 
43
- return Input as IInputComponentType<
44
- RootProps,
45
- IconProps,
46
- SlotProps,
47
- FieldProps,
48
- RootRef,
49
- FieldRef
50
- >;
51
- };
39
+ return Object.assign(Input, {
40
+ Icon,
41
+ Slot,
42
+ Field,
43
+ }) as IInputComponentType<RootProps, IconProps, SlotProps, FieldProps, RootRef, FieldRef>;
44
+ }
@@ -1,5 +1,6 @@
1
1
  import { createContext, useContext } from 'react';
2
- import type { InteractionState, SelectContextValue, SelectItemInteractionState } from './types';
2
+ import type { InteractionState } from '../types';
3
+ import type { SelectContextValue, SelectItemInteractionState } from './types';
3
4
 
4
5
  const defaultContextValue: SelectContextValue = {
5
6
  open: false,
@@ -1,16 +1,7 @@
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';
14
5
 
15
6
  export interface SelectItemInteractionState extends InteractionState {
16
7
  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
+ }
@@ -17,10 +17,8 @@ export function dataAttributes(attrs: Record<string, DataAttributeValue>): {
17
17
  const dataSet: Record<string, boolean | string> = {};
18
18
  for (const key in attrs) {
19
19
  const value = attrs[key];
20
- if (value === undefined) {
21
- continue;
22
- }
23
- dataSet[key] = value;
20
+
21
+ dataSet[key] = value ?? false;
24
22
  }
25
23
  return { dataSet };
26
24
  }