@rothko-ui/radio 1.0.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/LICENSE +21 -0
- package/README.md +0 -0
- package/dist/Radio.d.ts +8 -0
- package/dist/RadioGroup.d.ts +101 -0
- package/dist/RadioGroupContext.d.ts +12 -0
- package/dist/RadioInner.d.ts +54 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.mjs +469 -0
- package/dist/useRadioGroup.d.ts +2 -0
- package/package.json +53 -0
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2022 rothko-ui
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
File without changes
|
package/dist/Radio.d.ts
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
import type { WithAria } from '@rothko-ui/system';
|
2
|
+
import React from 'react';
|
3
|
+
import type { AriaAttributes, RadioInnerProps } from './RadioInner';
|
4
|
+
type RadioProps = Pick<WithAria<RadioInnerProps, AriaAttributes>, 'id' | 'children' | 'className' | 'disabled' | 'style' | 'aria-label' | 'aria-describedby' | 'aria-details' | 'aria-labelledby' | 'aria-controls' | 'aria-errormessage'> & {
|
5
|
+
$key: string;
|
6
|
+
};
|
7
|
+
declare const Radio: ({ id, $key, children, className, disabled, style, "aria-label": ariaLabel, "aria-describedby": ariaDescribedBy, "aria-details": ariaDetails, "aria-labelledby": ariaLabelledBy, "aria-controls": ariaControls, "aria-errormessage": ariaErrorMessage, }: RadioProps) => React.JSX.Element;
|
8
|
+
export default Radio;
|
@@ -0,0 +1,101 @@
|
|
1
|
+
import type { RothkoKind, Size, WithAria, Dictionary } from '@rothko-ui/system';
|
2
|
+
import React from 'react';
|
3
|
+
type StyleableComponents = 'errorText' | 'label';
|
4
|
+
type AriaAttributes = 'aria-label' | 'aria-describedby' | 'aria-details' | 'aria-labelledby' | 'aria-hidden' | 'aria-controls' | 'aria-disabled' | 'aria-invalid' | 'aria-required' | 'aria-errormessage';
|
5
|
+
type RadioGroupProps<K extends string> = {
|
6
|
+
/**
|
7
|
+
* The `id` attribute of the radio group.
|
8
|
+
* @type {string}
|
9
|
+
*/
|
10
|
+
id?: string;
|
11
|
+
/**
|
12
|
+
* The content to be rendered inside the radio group.
|
13
|
+
* @type {React.ReactNode}
|
14
|
+
* @required
|
15
|
+
*/
|
16
|
+
children: React.ReactNode;
|
17
|
+
/**
|
18
|
+
* CSS class name(s).
|
19
|
+
* @type {string}
|
20
|
+
*/
|
21
|
+
className?: string;
|
22
|
+
/**
|
23
|
+
* Additional class names for the radio group components.
|
24
|
+
* @type {Object<StyleableComponents, string>}
|
25
|
+
*/
|
26
|
+
classNames?: Dictionary<StyleableComponents, string>;
|
27
|
+
/**
|
28
|
+
* The gap between columns in the radio group.
|
29
|
+
* @type {Size | number}
|
30
|
+
* @default '0.5rem'
|
31
|
+
*/
|
32
|
+
columnGap?: Size | number;
|
33
|
+
/**
|
34
|
+
* Specifies whether the radio group is disabled.
|
35
|
+
* @type {boolean}
|
36
|
+
* @default false
|
37
|
+
*/
|
38
|
+
disabled?: boolean;
|
39
|
+
/**
|
40
|
+
* Specifies whether the radio group has an error state.
|
41
|
+
*/
|
42
|
+
error?: boolean;
|
43
|
+
/**
|
44
|
+
* The error message to be displayed when the radio group has an error state.
|
45
|
+
* @type {string}
|
46
|
+
* @default 'Invalid'
|
47
|
+
*/
|
48
|
+
errorText?: string;
|
49
|
+
/**
|
50
|
+
* The gap between elements in the radio group.
|
51
|
+
* @type {Size | number}
|
52
|
+
* @default '0.5rem'
|
53
|
+
*/
|
54
|
+
gap?: Size | number;
|
55
|
+
/**
|
56
|
+
* The radio group's semantic style.
|
57
|
+
* @type {RothkoKind}
|
58
|
+
*/
|
59
|
+
kind?: RothkoKind;
|
60
|
+
/**
|
61
|
+
* The label for the radio group.
|
62
|
+
* @type {string}
|
63
|
+
*/
|
64
|
+
label?: string;
|
65
|
+
/**
|
66
|
+
* The maximum number of columns in the radio group.
|
67
|
+
* @type {number}
|
68
|
+
* @default 4
|
69
|
+
*/
|
70
|
+
maxCol?: number;
|
71
|
+
/**
|
72
|
+
* The callback function called when the value of the radio group changes.
|
73
|
+
* @param $key - The new value of the radio group.
|
74
|
+
*/
|
75
|
+
onChange: ($key: K) => void;
|
76
|
+
/**
|
77
|
+
* Specifies whether the radio group is required.
|
78
|
+
* @type {boolean}
|
79
|
+
*/
|
80
|
+
required?: boolean;
|
81
|
+
/**
|
82
|
+
* The gap between rows in the radio group.
|
83
|
+
* @default '0.5rem'
|
84
|
+
*/
|
85
|
+
rowGap?: Size | number;
|
86
|
+
/**
|
87
|
+
* The inline style for the radio group.
|
88
|
+
*/
|
89
|
+
style?: React.CSSProperties;
|
90
|
+
/**
|
91
|
+
* Additional inline styles for the radio group components.
|
92
|
+
* @type {Object<StyleableComponents, React.CSSProperties>}
|
93
|
+
*/
|
94
|
+
styles?: Dictionary<StyleableComponents, React.CSSProperties>;
|
95
|
+
/**
|
96
|
+
* The current value of the radio group.
|
97
|
+
*/
|
98
|
+
value?: K | null;
|
99
|
+
};
|
100
|
+
declare function RadioGroup<K extends string>({ id, 'aria-label': ariaLabel, 'aria-describedby': ariaDescribedBy, 'aria-details': ariaDetails, 'aria-labelledby': ariaLabelledBy, 'aria-hidden': ariaHidden, 'aria-controls': ariaControls, 'aria-disabled': ariaDisabled, 'aria-invalid': ariaInvalid, 'aria-required': ariaRequired, 'aria-errormessage': ariaErrorMessage, errorText, style, styles, className, classNames, maxCol, columnGap, rowGap, value, kind, onChange, error, label, disabled, children, }: WithAria<RadioGroupProps<K>, AriaAttributes>): React.JSX.Element;
|
101
|
+
export default RadioGroup;
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import type { RothkoKind } from '@rothko-ui/system';
|
2
|
+
export type RadioGroupContextType = {
|
3
|
+
radioGroupLabelId: string;
|
4
|
+
radioGroupErrorId: string;
|
5
|
+
selectedValue?: string | null;
|
6
|
+
error?: boolean;
|
7
|
+
disabled?: boolean;
|
8
|
+
onChange: ($key: string) => void;
|
9
|
+
kind?: RothkoKind;
|
10
|
+
};
|
11
|
+
declare const RadioGroupContext: import("react").Context<RadioGroupContextType | null>;
|
12
|
+
export default RadioGroupContext;
|
@@ -0,0 +1,54 @@
|
|
1
|
+
import type { RothkoKind, WithAria } from '@rothko-ui/system';
|
2
|
+
import React from 'react';
|
3
|
+
export type AriaAttributes = 'aria-label' | 'aria-describedby' | 'aria-details' | 'aria-labelledby' | 'aria-controls' | 'aria-errormessage';
|
4
|
+
export type RadioInnerProps = {
|
5
|
+
/**
|
6
|
+
* The `id` attribute of the radio component.
|
7
|
+
* @type {string}
|
8
|
+
*/
|
9
|
+
id?: string;
|
10
|
+
/**
|
11
|
+
* The content to be rendered inside the radio component.
|
12
|
+
* @type {React.ReactNode}
|
13
|
+
*/
|
14
|
+
children?: React.ReactNode;
|
15
|
+
/**
|
16
|
+
* CSS class name(s).
|
17
|
+
* @type {string}
|
18
|
+
*/
|
19
|
+
className?: string;
|
20
|
+
/**
|
21
|
+
* Specifies whether the radio component is disabled.
|
22
|
+
* @type {boolean}
|
23
|
+
* @default false
|
24
|
+
*/
|
25
|
+
disabled?: boolean;
|
26
|
+
/**
|
27
|
+
* Specifies whether the radio component has an error.
|
28
|
+
* @type {boolean}
|
29
|
+
* @default false
|
30
|
+
*/
|
31
|
+
error?: boolean;
|
32
|
+
/**
|
33
|
+
* The radio component's semantic style.
|
34
|
+
* @type {RothkoKind}
|
35
|
+
*/
|
36
|
+
kind?: RothkoKind;
|
37
|
+
/**
|
38
|
+
* The callback function to be called when the radio component is selected.
|
39
|
+
*/
|
40
|
+
onSelect: () => void;
|
41
|
+
/**
|
42
|
+
* Specifies whether the radio component is selected.
|
43
|
+
* @type {boolean}
|
44
|
+
* @default false
|
45
|
+
*/
|
46
|
+
selected?: boolean;
|
47
|
+
/**
|
48
|
+
* The inline style for the radio component.
|
49
|
+
* @type {React.CSSProperties}
|
50
|
+
*/
|
51
|
+
style?: React.CSSProperties;
|
52
|
+
};
|
53
|
+
declare const RadioInner: ({ id, "aria-label": ariaLabel, "aria-describedby": ariaDescribedBy, "aria-details": ariaDetails, "aria-labelledby": ariaLabelledBy, "aria-controls": ariaControls, "aria-errormessage": ariaErrorMessage, children, className, disabled, error, kind, onSelect, selected, style, }: WithAria<RadioInnerProps, AriaAttributes>) => React.JSX.Element;
|
54
|
+
export default RadioInner;
|
package/dist/index.d.ts
ADDED
package/dist/index.mjs
ADDED
@@ -0,0 +1,469 @@
|
|
1
|
+
import e__default, { useRef, createContext, useContext } from 'react';
|
2
|
+
|
3
|
+
const classes$1 = (...args) => {
|
4
|
+
const classNames = [];
|
5
|
+
for (const arg of args) {
|
6
|
+
if (typeof arg === 'string' && arg !== '') {
|
7
|
+
classNames.push(arg);
|
8
|
+
} else if (typeof arg === 'object' && arg !== null) {
|
9
|
+
classNames.push(...Object.keys(arg).filter(key => Boolean(arg[key])));
|
10
|
+
}
|
11
|
+
}
|
12
|
+
return classNames.join(' ');
|
13
|
+
};
|
14
|
+
|
15
|
+
const extractAriaProps = ({
|
16
|
+
'aria-activedescendant': ariaActivedescendant,
|
17
|
+
'aria-atomic': ariaAtomic,
|
18
|
+
'aria-autocomplete': ariaAutocomplete,
|
19
|
+
'aria-busy': ariaBusy,
|
20
|
+
'aria-checked': ariaChecked,
|
21
|
+
'aria-colcount': ariaColcount,
|
22
|
+
'aria-colindex': ariaColindex,
|
23
|
+
'aria-colspan': ariaColspan,
|
24
|
+
'aria-controls': ariaControls,
|
25
|
+
'aria-current': ariaCurrent,
|
26
|
+
'aria-describedby': ariaDescribedby,
|
27
|
+
'aria-description': ariaDescription,
|
28
|
+
'aria-details': ariaDetails,
|
29
|
+
'aria-disabled': ariaDisabled,
|
30
|
+
'aria-errormessage': ariaErrormessage,
|
31
|
+
'aria-expanded': ariaExpanded,
|
32
|
+
'aria-flowto': ariaFlowto,
|
33
|
+
'aria-haspopup': ariaHaspopup,
|
34
|
+
'aria-hidden': ariaHidden,
|
35
|
+
'aria-invalid': ariaInvalid,
|
36
|
+
'aria-keyshortcuts': ariaKeyshortcuts,
|
37
|
+
'aria-label': ariaLabel,
|
38
|
+
'aria-labelledby': ariaLabelledby,
|
39
|
+
'aria-level': ariaLevel,
|
40
|
+
'aria-live': ariaLive,
|
41
|
+
'aria-modal': ariaModal,
|
42
|
+
'aria-multiline': ariaMultiline,
|
43
|
+
'aria-multiselectable': ariaMultiselectable,
|
44
|
+
'aria-setsize': ariaSetsize,
|
45
|
+
'aria-sort': ariaSort,
|
46
|
+
'aria-valuemax': ariaValuemax,
|
47
|
+
'aria-valuemin': ariaValuemin,
|
48
|
+
'aria-valuenow': ariaValuenow,
|
49
|
+
'aria-valuetext': ariaValuetext,
|
50
|
+
'aria-orientation': ariaOrientation,
|
51
|
+
'aria-owns': ariaOwns,
|
52
|
+
'aria-placeholder': ariaPlaceholder,
|
53
|
+
'aria-posinset': ariaPosinset,
|
54
|
+
'aria-pressed': ariaPressed,
|
55
|
+
'aria-readonly': ariaReadonly,
|
56
|
+
'aria-relevant': ariaRelevant,
|
57
|
+
'aria-required': ariaRequired,
|
58
|
+
'aria-roledescription': ariaRoledescription,
|
59
|
+
'aria-rowcount': ariaRowcount,
|
60
|
+
'aria-rowindex': ariaRowindex,
|
61
|
+
'aria-rowspan': ariaRowspan,
|
62
|
+
'aria-selected': ariaSelected,
|
63
|
+
...props
|
64
|
+
}) => {
|
65
|
+
return {
|
66
|
+
ariaAttributes: {
|
67
|
+
'aria-activedescendant': ariaActivedescendant,
|
68
|
+
'aria-atomic': ariaAtomic,
|
69
|
+
'aria-autocomplete': ariaAutocomplete,
|
70
|
+
'aria-busy': ariaBusy,
|
71
|
+
'aria-checked': ariaChecked,
|
72
|
+
'aria-colcount': ariaColcount,
|
73
|
+
'aria-colindex': ariaColindex,
|
74
|
+
'aria-colspan': ariaColspan,
|
75
|
+
'aria-controls': ariaControls,
|
76
|
+
'aria-current': ariaCurrent,
|
77
|
+
'aria-describedby': ariaDescribedby,
|
78
|
+
'aria-description': ariaDescription,
|
79
|
+
'aria-details': ariaDetails,
|
80
|
+
'aria-disabled': ariaDisabled,
|
81
|
+
'aria-errormessage': ariaErrormessage,
|
82
|
+
'aria-expanded': ariaExpanded,
|
83
|
+
'aria-flowto': ariaFlowto,
|
84
|
+
'aria-haspopup': ariaHaspopup,
|
85
|
+
'aria-hidden': ariaHidden,
|
86
|
+
'aria-invalid': ariaInvalid,
|
87
|
+
'aria-keyshortcuts': ariaKeyshortcuts,
|
88
|
+
'aria-label': ariaLabel,
|
89
|
+
'aria-labelledby': ariaLabelledby,
|
90
|
+
'aria-level': ariaLevel,
|
91
|
+
'aria-live': ariaLive,
|
92
|
+
'aria-modal': ariaModal,
|
93
|
+
'aria-multiline': ariaMultiline,
|
94
|
+
'aria-multiselectable': ariaMultiselectable,
|
95
|
+
'aria-setsize': ariaSetsize,
|
96
|
+
'aria-sort': ariaSort,
|
97
|
+
'aria-valuemax': ariaValuemax,
|
98
|
+
'aria-valuemin': ariaValuemin,
|
99
|
+
'aria-valuenow': ariaValuenow,
|
100
|
+
'aria-valuetext': ariaValuetext,
|
101
|
+
'aria-orientation': ariaOrientation,
|
102
|
+
'aria-owns': ariaOwns,
|
103
|
+
'aria-placeholder': ariaPlaceholder,
|
104
|
+
'aria-posinset': ariaPosinset,
|
105
|
+
'aria-pressed': ariaPressed,
|
106
|
+
'aria-readonly': ariaReadonly,
|
107
|
+
'aria-relevant': ariaRelevant,
|
108
|
+
'aria-required': ariaRequired,
|
109
|
+
'aria-roledescription': ariaRoledescription,
|
110
|
+
'aria-rowcount': ariaRowcount,
|
111
|
+
'aria-rowindex': ariaRowindex,
|
112
|
+
'aria-rowspan': ariaRowspan,
|
113
|
+
'aria-selected': ariaSelected
|
114
|
+
},
|
115
|
+
props
|
116
|
+
};
|
117
|
+
};
|
118
|
+
|
119
|
+
/* eslint-disable react/no-unused-prop-types */
|
120
|
+
const Container = /*#__PURE__*/e__default.forwardRef((props, ref) => {
|
121
|
+
const {
|
122
|
+
props: {
|
123
|
+
as = 'div',
|
124
|
+
id,
|
125
|
+
children,
|
126
|
+
onBlur,
|
127
|
+
onFocus,
|
128
|
+
onClick,
|
129
|
+
className,
|
130
|
+
role,
|
131
|
+
...style
|
132
|
+
},
|
133
|
+
ariaAttributes
|
134
|
+
} = extractAriaProps(props);
|
135
|
+
return /*#__PURE__*/e__default.createElement(as, {
|
136
|
+
...ariaAttributes,
|
137
|
+
id,
|
138
|
+
ref,
|
139
|
+
onClick,
|
140
|
+
onBlur,
|
141
|
+
onFocus,
|
142
|
+
className,
|
143
|
+
style,
|
144
|
+
role
|
145
|
+
}, children);
|
146
|
+
});
|
147
|
+
Container.displayName = 'Container';
|
148
|
+
|
149
|
+
/* eslint-disable react/no-unused-prop-types */
|
150
|
+
const Grid = /*#__PURE__*/e__default.forwardRef((props, ref) => {
|
151
|
+
const {
|
152
|
+
props: {
|
153
|
+
as = 'div',
|
154
|
+
role,
|
155
|
+
onBlur,
|
156
|
+
onClick,
|
157
|
+
onFocus,
|
158
|
+
children,
|
159
|
+
className,
|
160
|
+
...style
|
161
|
+
},
|
162
|
+
ariaAttributes
|
163
|
+
} = extractAriaProps(props);
|
164
|
+
return /*#__PURE__*/e__default.createElement(as, {
|
165
|
+
...ariaAttributes,
|
166
|
+
ref,
|
167
|
+
onClick,
|
168
|
+
onFocus,
|
169
|
+
onBlur,
|
170
|
+
className: classes$1(className, 'grid'),
|
171
|
+
style,
|
172
|
+
role
|
173
|
+
}, children);
|
174
|
+
});
|
175
|
+
Grid.displayName = 'Grid';
|
176
|
+
|
177
|
+
/* eslint-disable react/no-unused-prop-types */
|
178
|
+
const FlexItem = /*#__PURE__*/e__default.forwardRef((props, ref) => {
|
179
|
+
const {
|
180
|
+
props: {
|
181
|
+
as = 'div',
|
182
|
+
children,
|
183
|
+
className,
|
184
|
+
onClick,
|
185
|
+
onBlur,
|
186
|
+
onFocus,
|
187
|
+
role,
|
188
|
+
...style
|
189
|
+
},
|
190
|
+
ariaAttributes
|
191
|
+
} = extractAriaProps(props);
|
192
|
+
return /*#__PURE__*/e__default.createElement(as, {
|
193
|
+
...ariaAttributes,
|
194
|
+
ref,
|
195
|
+
onClick,
|
196
|
+
onFocus,
|
197
|
+
onBlur,
|
198
|
+
className: classes$1(className, 'flex-1 shrink-0 basis-auto'),
|
199
|
+
style,
|
200
|
+
role
|
201
|
+
}, children);
|
202
|
+
});
|
203
|
+
FlexItem.displayName = 'FlexItem';
|
204
|
+
|
205
|
+
/* eslint-disable react/no-unused-prop-types */
|
206
|
+
const Flex = /*#__PURE__*/e__default.forwardRef((props, ref) => {
|
207
|
+
const {
|
208
|
+
props: {
|
209
|
+
id,
|
210
|
+
as = 'div',
|
211
|
+
children,
|
212
|
+
className,
|
213
|
+
role,
|
214
|
+
onClick,
|
215
|
+
onBlur,
|
216
|
+
onFocus,
|
217
|
+
...style
|
218
|
+
},
|
219
|
+
ariaAttributes
|
220
|
+
} = extractAriaProps(props);
|
221
|
+
return /*#__PURE__*/e__default.createElement(as, {
|
222
|
+
...ariaAttributes,
|
223
|
+
id,
|
224
|
+
ref,
|
225
|
+
onClick,
|
226
|
+
onFocus,
|
227
|
+
onBlur,
|
228
|
+
className: classes$1(className, 'flex'),
|
229
|
+
style,
|
230
|
+
role
|
231
|
+
}, children);
|
232
|
+
});
|
233
|
+
Flex.displayName = 'Flex';
|
234
|
+
|
235
|
+
const debugFactory = module => {
|
236
|
+
const debuggerActive = Boolean(typeof process !== 'undefined' && process.env?.ROTHKO_DEBUG);
|
237
|
+
return (...argz) => {
|
238
|
+
if (debuggerActive) {
|
239
|
+
// eslint-disable-next-line no-console
|
240
|
+
console.log(`[rokthko-ui${`:${module}` }]`, ...argz);
|
241
|
+
}
|
242
|
+
};
|
243
|
+
};
|
244
|
+
|
245
|
+
const byteToHex = [];
|
246
|
+
for (let i = 0; i < 256; ++i) {
|
247
|
+
byteToHex.push((i + 0x100).toString(16).slice(1));
|
248
|
+
}
|
249
|
+
function unsafeStringify(arr, offset = 0) {
|
250
|
+
return (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + '-' + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + '-' + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + '-' + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + '-' + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase();
|
251
|
+
}
|
252
|
+
let getRandomValues;
|
253
|
+
const rnds8 = new Uint8Array(16);
|
254
|
+
function rng() {
|
255
|
+
if (!getRandomValues) {
|
256
|
+
if (typeof crypto === 'undefined' || !crypto.getRandomValues) {
|
257
|
+
throw new Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');
|
258
|
+
}
|
259
|
+
getRandomValues = crypto.getRandomValues.bind(crypto);
|
260
|
+
}
|
261
|
+
return getRandomValues(rnds8);
|
262
|
+
}
|
263
|
+
const randomUUID = typeof crypto !== 'undefined' && crypto.randomUUID && crypto.randomUUID.bind(crypto);
|
264
|
+
var native = {
|
265
|
+
randomUUID
|
266
|
+
};
|
267
|
+
function v4(options, buf, offset) {
|
268
|
+
if (native.randomUUID && true && !options) {
|
269
|
+
return native.randomUUID();
|
270
|
+
}
|
271
|
+
options = options || {};
|
272
|
+
const rnds = options.random ?? options.rng?.() ?? rng();
|
273
|
+
if (rnds.length < 16) {
|
274
|
+
throw new Error('Random bytes length must be >= 16');
|
275
|
+
}
|
276
|
+
rnds[6] = rnds[6] & 0x0f | 0x40;
|
277
|
+
rnds[8] = rnds[8] & 0x3f | 0x80;
|
278
|
+
return unsafeStringify(rnds);
|
279
|
+
}
|
280
|
+
|
281
|
+
// I tried using the React.useId() hook but
|
282
|
+
// that caused problems in the dropdown selectQuery
|
283
|
+
const useId = id => {
|
284
|
+
const randomId = useRef(v4());
|
285
|
+
return `id-${randomId.current}`;
|
286
|
+
};
|
287
|
+
var Direction;
|
288
|
+
(function (Direction) {
|
289
|
+
Direction[Direction["INCR"] = 1] = "INCR";
|
290
|
+
Direction[Direction["DECR"] = -1] = "DECR";
|
291
|
+
})(Direction || (Direction = {}));
|
292
|
+
debugFactory('useOptions');
|
293
|
+
|
294
|
+
const freeGlobal = typeof global === 'object' && global !== null && global.Object === Object && global;
|
295
|
+
const freeGlobalThis = typeof globalThis === 'object' && globalThis !== null && globalThis.Object == Object && globalThis;
|
296
|
+
const freeSelf = typeof self === 'object' && self !== null && self.Object === Object && self;
|
297
|
+
freeGlobalThis || freeGlobal || freeSelf || Function('return this')();
|
298
|
+
|
299
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
300
|
+
const getTag = value => {
|
301
|
+
if (value == null) {
|
302
|
+
return value === undefined ? '[object Undefined]' : '[object Null]';
|
303
|
+
}
|
304
|
+
return Object.prototype.toString.call(value);
|
305
|
+
};
|
306
|
+
/**
|
307
|
+
* Checks if the given value is a string.
|
308
|
+
*
|
309
|
+
* @param {any} value The value to check.
|
310
|
+
* @returns {boolean} Returns true if the value is a string, else false.
|
311
|
+
* @example
|
312
|
+
*
|
313
|
+
* isString("hello")
|
314
|
+
* // => true
|
315
|
+
*/
|
316
|
+
const isString = value => {
|
317
|
+
return typeof value === 'string' || typeof value === 'object' && value != null && !Array.isArray(value) && getTag(value) == '[object String]';
|
318
|
+
};
|
319
|
+
|
320
|
+
const ListenableKeys = {
|
321
|
+
Enter: 13,
|
322
|
+
Tab: 9,
|
323
|
+
Backspace: 8,
|
324
|
+
Escape: 27,
|
325
|
+
Space: 32,
|
326
|
+
ArrowUp: 38,
|
327
|
+
ArrowDown: 40,
|
328
|
+
ArrowLeft: 37,
|
329
|
+
ArrowRight: 39,
|
330
|
+
Shift: 16,
|
331
|
+
Control: 17,
|
332
|
+
Alt: 18,
|
333
|
+
Insert: 45,
|
334
|
+
Delete: 46,
|
335
|
+
PageUp: 33,
|
336
|
+
PageDown: 34,
|
337
|
+
Clear: 12
|
338
|
+
};
|
339
|
+
const keyDownFactory = handlerMap => {
|
340
|
+
return e => {
|
341
|
+
// get code
|
342
|
+
const code = ListenableKeys[e.key];
|
343
|
+
if (typeof code === 'undefined') return;
|
344
|
+
const handler = handlerMap[code];
|
345
|
+
if (handler) {
|
346
|
+
e.preventDefault();
|
347
|
+
handler(e);
|
348
|
+
}
|
349
|
+
};
|
350
|
+
};
|
351
|
+
|
352
|
+
const classes = (...args) => {
|
353
|
+
const classNames = [];
|
354
|
+
for (const arg of args) {
|
355
|
+
if (typeof arg === 'string' && arg !== '') {
|
356
|
+
classNames.push(arg);
|
357
|
+
} else if (typeof arg === 'object' && arg !== null) {
|
358
|
+
classNames.push(...Object.keys(arg).filter(key => Boolean(arg[key])));
|
359
|
+
}
|
360
|
+
}
|
361
|
+
return classNames.join(' ');
|
362
|
+
};
|
363
|
+
const Paragraph = ({
|
364
|
+
as,
|
365
|
+
children,
|
366
|
+
className,
|
367
|
+
kind,
|
368
|
+
size = 'm',
|
369
|
+
style = {},
|
370
|
+
variant = 'regular',
|
371
|
+
...props
|
372
|
+
}) => {
|
373
|
+
const colorVarStyle = {
|
374
|
+
'--paragraph-color': kind ? `var(--rothko-${kind})` : 'var(--rothko-typography-body-color)'
|
375
|
+
};
|
376
|
+
const paragraphClasses = classes('my-[0.125rem] mx-0', variant === 'italic' && 'rothko-font-italic', variant === 'bold' && 'rothko-font-bold', variant === 'light' && 'rothko-font-light', variant === 'regular' && 'rothko-font-regular', size === 'xs' && 'rothko-paragraph-size-xs', size === 's' && 'rothko-paragraph-size-s', size === 'm' && 'rothko-paragraph-size-m', size === 'l' && 'rothko-paragraph-size-l', 'text-(--paragraph-color)', className);
|
377
|
+
return /*#__PURE__*/e__default.createElement(as || 'p', {
|
378
|
+
...props,
|
379
|
+
className: paragraphClasses,
|
380
|
+
style: {
|
381
|
+
...style,
|
382
|
+
...colorVarStyle
|
383
|
+
}
|
384
|
+
}, children);
|
385
|
+
};
|
386
|
+
const Label = ({
|
387
|
+
as,
|
388
|
+
children,
|
389
|
+
className,
|
390
|
+
kind,
|
391
|
+
style = {},
|
392
|
+
...props
|
393
|
+
}) => {
|
394
|
+
const colorVarStyle = {
|
395
|
+
'--label-color': kind ? `var(--rothko-${kind})` : 'var(--rothko-typography-body-color)'
|
396
|
+
};
|
397
|
+
const labelClasses = classes('my-[0.125rem] mx-0', 'rothko-font-regular', 'rothko-paragraph-size-xs', 'text-(--label-color)', 'uppercase', 'tracking-[0.0625rem]',
|
398
|
+
// letter-spacing: 0.0625rem;
|
399
|
+
className);
|
400
|
+
return /*#__PURE__*/e__default.createElement(as || 'label', {
|
401
|
+
...props,
|
402
|
+
className: labelClasses,
|
403
|
+
style: {
|
404
|
+
...style,
|
405
|
+
...colorVarStyle
|
406
|
+
}
|
407
|
+
}, children);
|
408
|
+
};
|
409
|
+
|
410
|
+
const RadioInner = ({ id, 'aria-label': ariaLabel, 'aria-describedby': ariaDescribedBy, 'aria-details': ariaDetails, 'aria-labelledby': ariaLabelledBy, 'aria-controls': ariaControls, 'aria-errormessage': ariaErrorMessage, children, className, disabled, error, kind, onSelect, selected, style = {}, }) => {
|
411
|
+
const labelId = useId();
|
412
|
+
const clickRadio = () => {
|
413
|
+
if (disabled)
|
414
|
+
return;
|
415
|
+
onSelect();
|
416
|
+
};
|
417
|
+
const onKeyDown = keyDownFactory({ [ListenableKeys.Enter]: clickRadio });
|
418
|
+
const radioVarStyle = {
|
419
|
+
'--radio-selected-background': kind
|
420
|
+
? `var(--rothko-${kind})`
|
421
|
+
: 'var(--rothko-radio-background-focus)',
|
422
|
+
};
|
423
|
+
const radioContainerClassnames = classes$1('flex', 'items-center', 'justify-start', 'gap-[0.3rem]', 'rothko-color-body', 'rothko-font-regular', 'rothko-paragraph-size-default', className);
|
424
|
+
const radioOuterCircleClassnames = classes$1('w-[1.25rem]', 'h-[1.25rem]', 'rounded-full', 'p-[0.125rem]', !disabled && 'cursor-pointer', 'bg-(--rothko-radio-border)', disabled && 'cursor-not-allowed', disabled && 'opacity-60', error && 'outline outline-[1.5px] outline-[var(--danger-500)] outline-offset-[0.5px]');
|
425
|
+
const radioMiddleCircleClassnames = classes$1('w-full', 'h-full', 'rounded-full', 'p-[0.25rem]', !selected && 'bg-(--rothko-radio-background)', 'transition-[background-color 0.1s ease]', !error && selected && 'bg-(--radio-selected-background)', error && selected && 'bg-(--rothko-danger-500)');
|
426
|
+
const radioInnerCircleClassnames = 'w-full h-full rounded-full bg-(--rothko-radio-border)';
|
427
|
+
return (e__default.createElement("div", { style: { ...style, ...radioVarStyle }, className: radioContainerClassnames },
|
428
|
+
e__default.createElement("div", { id: id, "aria-describedby": ariaDescribedBy, "aria-details": ariaDetails, "aria-labelledby": !ariaLabelledBy && children ? labelId : ariaLabelledBy, "aria-controls": ariaControls, "aria-checked": !!selected, "aria-disabled": disabled, "aria-label": ariaLabel, "aria-errormessage": ariaErrorMessage, role: "radio", className: radioOuterCircleClassnames, onClick: () => clickRadio(), onKeyDown: onKeyDown, tabIndex: 0 },
|
429
|
+
e__default.createElement("div", { "aria-hidden": true, className: radioMiddleCircleClassnames }, selected && e__default.createElement("div", { "aria-hidden": true, className: radioInnerCircleClassnames }))),
|
430
|
+
children &&
|
431
|
+
(typeof children === 'string' ? (e__default.createElement(Paragraph, { id: labelId }, children)) : (e__default.createElement("div", { id: labelId }, children)))));
|
432
|
+
};
|
433
|
+
|
434
|
+
const RadioGroupContext = createContext(null);
|
435
|
+
|
436
|
+
const useRadioGroup = () => {
|
437
|
+
const ctx = useContext(RadioGroupContext);
|
438
|
+
if (!ctx) {
|
439
|
+
throw new Error('useRadioGroup called outside of context');
|
440
|
+
}
|
441
|
+
return ctx;
|
442
|
+
};
|
443
|
+
|
444
|
+
const Radio = ({ id, $key, children, className, disabled, style, 'aria-label': ariaLabel, 'aria-describedby': ariaDescribedBy, 'aria-details': ariaDetails, 'aria-labelledby': ariaLabelledBy, 'aria-controls': ariaControls, 'aria-errormessage': ariaErrorMessage, }) => {
|
445
|
+
const childrenStringLabel = isString(children) ? children : undefined;
|
446
|
+
const { radioGroupErrorId, selectedValue, onChange, error, disabled: groupDisabled, kind, } = useRadioGroup();
|
447
|
+
return (e__default.createElement(RadioInner, { "aria-controls": ariaControls, "aria-describedby": ariaDescribedBy, "aria-details": ariaDetails, "aria-errormessage": !ariaErrorMessage && error ? radioGroupErrorId : ariaErrorMessage, "aria-label": ariaLabel || childrenStringLabel, "aria-labelledby": ariaLabelledBy, className: className, disabled: disabled || groupDisabled, error: error, id: id, kind: kind, onSelect: () => onChange($key), selected: $key === selectedValue, style: style }, children));
|
448
|
+
};
|
449
|
+
|
450
|
+
function RadioGroup({ id, 'aria-label': ariaLabel, 'aria-describedby': ariaDescribedBy, 'aria-details': ariaDetails, 'aria-labelledby': ariaLabelledBy, 'aria-hidden': ariaHidden, 'aria-controls': ariaControls, 'aria-disabled': ariaDisabled, 'aria-invalid': ariaInvalid, 'aria-required': ariaRequired, 'aria-errormessage': ariaErrorMessage, errorText = 'Invalid', style, styles = {}, className, classNames = {}, maxCol = 4, columnGap = '0.5rem', rowGap = '0.5rem', value, kind, onChange, error, label, disabled, children, }) {
|
451
|
+
const labelId = useId();
|
452
|
+
const errorMessageId = useId();
|
453
|
+
const contextValue = e__default.useMemo(() => ({
|
454
|
+
radioGroupLabelId: labelId,
|
455
|
+
radioGroupErrorId: errorMessageId,
|
456
|
+
selectedValue: value,
|
457
|
+
onChange,
|
458
|
+
error,
|
459
|
+
kind,
|
460
|
+
disabled,
|
461
|
+
}), [value, error, kind, labelId, errorMessageId, disabled, onChange]);
|
462
|
+
return (e__default.createElement("div", { id: id, style: style, className: className },
|
463
|
+
label && (e__default.createElement(Label, { className: classNames.label, style: styles.label, id: labelId }, label)),
|
464
|
+
e__default.createElement(Grid, { "aria-label": ariaLabel, "aria-describedby": ariaDescribedBy, "aria-details": ariaDetails, "aria-labelledby": !ariaLabelledBy && label ? labelId : ariaLabelledBy, "aria-hidden": ariaHidden, "aria-invalid": ariaInvalid || error, "aria-controls": ariaControls, "aria-disabled": ariaDisabled, "aria-required": ariaRequired, "aria-errormessage": !ariaErrorMessage && error ? errorMessageId : ariaErrorMessage, role: "radiogroup", flexGrow: 1, gridTemplateColumns: `repeat(${maxCol}, 1fr)`, rowGap: rowGap, columnGap: columnGap },
|
465
|
+
e__default.createElement(RadioGroupContext.Provider, { value: contextValue }, children)),
|
466
|
+
error && (e__default.createElement(Paragraph, { className: classNames.errorText, style: styles.errorText, id: errorMessageId, kind: "danger" }, errorText))));
|
467
|
+
}
|
468
|
+
|
469
|
+
export { Radio, RadioGroup };
|
package/package.json
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
{
|
2
|
+
"name": "@rothko-ui/radio",
|
3
|
+
"version": "1.0.1",
|
4
|
+
"repository": {
|
5
|
+
"type": "git",
|
6
|
+
"url": "git+https://github.com/luxo-ai/rothko-ui.git",
|
7
|
+
"directory": "packages/react/src/Radio"
|
8
|
+
},
|
9
|
+
"homepage": "https://rothko-ui.com",
|
10
|
+
"publishConfig": {
|
11
|
+
"access": "public"
|
12
|
+
},
|
13
|
+
"private": false,
|
14
|
+
"license": "MIT",
|
15
|
+
"author": "Luis Serazo <luxo.ai@proton.me>",
|
16
|
+
"description": "Radio for Rothko UI",
|
17
|
+
"type": "module",
|
18
|
+
"types": "dist/index.d.ts",
|
19
|
+
"module": "dist/index.mjs",
|
20
|
+
"source": "src/index.ts",
|
21
|
+
"scripts": {
|
22
|
+
"build": "NODE_ENV=prod rollup --config",
|
23
|
+
"build:dev": "NODE_ENV=dev rollup --config --watch",
|
24
|
+
"generate:scss": "typed-scss-modules src --nameFormat none --exportType default",
|
25
|
+
"prebuild": "rimraf dist",
|
26
|
+
"typecheck": "tsc --noEmit"
|
27
|
+
},
|
28
|
+
"dependencies": {
|
29
|
+
"@rothko-ui/system": "^1.0.1"
|
30
|
+
},
|
31
|
+
"peerDependencies": {
|
32
|
+
"react": "^18.0.0",
|
33
|
+
"react-dom": "^18.2.0"
|
34
|
+
},
|
35
|
+
"devDependencies": {
|
36
|
+
"@babel/core": "^7.26.7",
|
37
|
+
"@rollup/plugin-babel": "^6.0.4",
|
38
|
+
"@rollup/plugin-commonjs": "^28.0.2",
|
39
|
+
"@rollup/plugin-node-resolve": "^16.0.0",
|
40
|
+
"@rollup/plugin-typescript": "^12.1.2",
|
41
|
+
"@types/react": "^18.0.0",
|
42
|
+
"@types/react-dom": "^18.0.0",
|
43
|
+
"rimraf": "^6.0.1",
|
44
|
+
"rollup": "^4.34.1",
|
45
|
+
"rollup-plugin-peer-deps-external": "^2.2.4",
|
46
|
+
"typescript": "^5.7.3"
|
47
|
+
},
|
48
|
+
"files": [
|
49
|
+
"dist",
|
50
|
+
"LICENSE",
|
51
|
+
"README.md"
|
52
|
+
]
|
53
|
+
}
|