@radix-ui/react-toggle-group 0.0.0-20250116175529

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/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # `react-toggle-group`
2
+
3
+ ## Installation
4
+
5
+ ```sh
6
+ $ yarn add @radix-ui/react-toggle-group
7
+ # or
8
+ $ npm install @radix-ui/react-toggle-group
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ View docs [here](https://radix-ui.com/primitives/docs/components/toggle-group).
@@ -0,0 +1,75 @@
1
+ import * as _radix_ui_react_context from '@radix-ui/react-context';
2
+ import React from 'react';
3
+ import { Primitive } from '@radix-ui/react-primitive';
4
+ import * as RovingFocusGroup from '@radix-ui/react-roving-focus';
5
+ import { Toggle } from '@radix-ui/react-toggle';
6
+
7
+ declare const createToggleGroupScope: _radix_ui_react_context.CreateScope;
8
+ interface ToggleGroupSingleProps extends ToggleGroupImplSingleProps {
9
+ type: 'single';
10
+ }
11
+ interface ToggleGroupMultipleProps extends ToggleGroupImplMultipleProps {
12
+ type: 'multiple';
13
+ }
14
+ declare const ToggleGroup: React.ForwardRefExoticComponent<(ToggleGroupSingleProps | ToggleGroupMultipleProps) & React.RefAttributes<HTMLDivElement>>;
15
+ interface ToggleGroupImplSingleProps extends ToggleGroupImplProps {
16
+ /**
17
+ * The controlled stateful value of the item that is pressed.
18
+ */
19
+ value?: string;
20
+ /**
21
+ * The value of the item that is pressed when initially rendered. Use
22
+ * `defaultValue` if you do not need to control the state of a toggle group.
23
+ */
24
+ defaultValue?: string;
25
+ /**
26
+ * The callback that fires when the value of the toggle group changes.
27
+ */
28
+ onValueChange?(value: string): void;
29
+ }
30
+ interface ToggleGroupImplMultipleProps extends ToggleGroupImplProps {
31
+ /**
32
+ * The controlled stateful value of the items that are pressed.
33
+ */
34
+ value?: string[];
35
+ /**
36
+ * The value of the items that are pressed when initially rendered. Use
37
+ * `defaultValue` if you do not need to control the state of a toggle group.
38
+ */
39
+ defaultValue?: string[];
40
+ /**
41
+ * The callback that fires when the state of the toggle group changes.
42
+ */
43
+ onValueChange?(value: string[]): void;
44
+ }
45
+ type RovingFocusGroupProps = React.ComponentPropsWithoutRef<typeof RovingFocusGroup.Root>;
46
+ type PrimitiveDivProps = React.ComponentPropsWithoutRef<typeof Primitive.div>;
47
+ interface ToggleGroupImplProps extends PrimitiveDivProps {
48
+ /**
49
+ * Whether the group is disabled from user interaction.
50
+ * @defaultValue false
51
+ */
52
+ disabled?: boolean;
53
+ /**
54
+ * Whether the group should maintain roving focus of its buttons.
55
+ * @defaultValue true
56
+ */
57
+ rovingFocus?: boolean;
58
+ loop?: RovingFocusGroupProps['loop'];
59
+ orientation?: RovingFocusGroupProps['orientation'];
60
+ dir?: RovingFocusGroupProps['dir'];
61
+ }
62
+ interface ToggleGroupItemProps extends Omit<ToggleGroupItemImplProps, 'pressed'> {
63
+ }
64
+ declare const ToggleGroupItem: React.ForwardRefExoticComponent<ToggleGroupItemProps & React.RefAttributes<HTMLButtonElement>>;
65
+ type ToggleProps = React.ComponentPropsWithoutRef<typeof Toggle>;
66
+ interface ToggleGroupItemImplProps extends Omit<ToggleProps, 'defaultPressed' | 'onPressedChange'> {
67
+ /**
68
+ * A string value for the toggle group item. All items within a toggle group should use a unique value.
69
+ */
70
+ value: string;
71
+ }
72
+ declare const Root: React.ForwardRefExoticComponent<(ToggleGroupSingleProps | ToggleGroupMultipleProps) & React.RefAttributes<HTMLDivElement>>;
73
+ declare const Item: React.ForwardRefExoticComponent<ToggleGroupItemProps & React.RefAttributes<HTMLButtonElement>>;
74
+
75
+ export { Item, Root, ToggleGroup, ToggleGroupItem, type ToggleGroupItemProps, type ToggleGroupMultipleProps, type ToggleGroupSingleProps, createToggleGroupScope };
@@ -0,0 +1,75 @@
1
+ import * as _radix_ui_react_context from '@radix-ui/react-context';
2
+ import React from 'react';
3
+ import { Primitive } from '@radix-ui/react-primitive';
4
+ import * as RovingFocusGroup from '@radix-ui/react-roving-focus';
5
+ import { Toggle } from '@radix-ui/react-toggle';
6
+
7
+ declare const createToggleGroupScope: _radix_ui_react_context.CreateScope;
8
+ interface ToggleGroupSingleProps extends ToggleGroupImplSingleProps {
9
+ type: 'single';
10
+ }
11
+ interface ToggleGroupMultipleProps extends ToggleGroupImplMultipleProps {
12
+ type: 'multiple';
13
+ }
14
+ declare const ToggleGroup: React.ForwardRefExoticComponent<(ToggleGroupSingleProps | ToggleGroupMultipleProps) & React.RefAttributes<HTMLDivElement>>;
15
+ interface ToggleGroupImplSingleProps extends ToggleGroupImplProps {
16
+ /**
17
+ * The controlled stateful value of the item that is pressed.
18
+ */
19
+ value?: string;
20
+ /**
21
+ * The value of the item that is pressed when initially rendered. Use
22
+ * `defaultValue` if you do not need to control the state of a toggle group.
23
+ */
24
+ defaultValue?: string;
25
+ /**
26
+ * The callback that fires when the value of the toggle group changes.
27
+ */
28
+ onValueChange?(value: string): void;
29
+ }
30
+ interface ToggleGroupImplMultipleProps extends ToggleGroupImplProps {
31
+ /**
32
+ * The controlled stateful value of the items that are pressed.
33
+ */
34
+ value?: string[];
35
+ /**
36
+ * The value of the items that are pressed when initially rendered. Use
37
+ * `defaultValue` if you do not need to control the state of a toggle group.
38
+ */
39
+ defaultValue?: string[];
40
+ /**
41
+ * The callback that fires when the state of the toggle group changes.
42
+ */
43
+ onValueChange?(value: string[]): void;
44
+ }
45
+ type RovingFocusGroupProps = React.ComponentPropsWithoutRef<typeof RovingFocusGroup.Root>;
46
+ type PrimitiveDivProps = React.ComponentPropsWithoutRef<typeof Primitive.div>;
47
+ interface ToggleGroupImplProps extends PrimitiveDivProps {
48
+ /**
49
+ * Whether the group is disabled from user interaction.
50
+ * @defaultValue false
51
+ */
52
+ disabled?: boolean;
53
+ /**
54
+ * Whether the group should maintain roving focus of its buttons.
55
+ * @defaultValue true
56
+ */
57
+ rovingFocus?: boolean;
58
+ loop?: RovingFocusGroupProps['loop'];
59
+ orientation?: RovingFocusGroupProps['orientation'];
60
+ dir?: RovingFocusGroupProps['dir'];
61
+ }
62
+ interface ToggleGroupItemProps extends Omit<ToggleGroupItemImplProps, 'pressed'> {
63
+ }
64
+ declare const ToggleGroupItem: React.ForwardRefExoticComponent<ToggleGroupItemProps & React.RefAttributes<HTMLButtonElement>>;
65
+ type ToggleProps = React.ComponentPropsWithoutRef<typeof Toggle>;
66
+ interface ToggleGroupItemImplProps extends Omit<ToggleProps, 'defaultPressed' | 'onPressedChange'> {
67
+ /**
68
+ * A string value for the toggle group item. All items within a toggle group should use a unique value.
69
+ */
70
+ value: string;
71
+ }
72
+ declare const Root: React.ForwardRefExoticComponent<(ToggleGroupSingleProps | ToggleGroupMultipleProps) & React.RefAttributes<HTMLDivElement>>;
73
+ declare const Item: React.ForwardRefExoticComponent<ToggleGroupItemProps & React.RefAttributes<HTMLButtonElement>>;
74
+
75
+ export { Item, Root, ToggleGroup, ToggleGroupItem, type ToggleGroupItemProps, type ToggleGroupMultipleProps, type ToggleGroupSingleProps, createToggleGroupScope };
package/dist/index.js ADDED
@@ -0,0 +1,207 @@
1
+ "use strict";
2
+ "use client";
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __export = (target, all) => {
10
+ for (var name in all)
11
+ __defProp(target, name, { get: all[name], enumerable: true });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key) && key !== except)
17
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
+ }
19
+ return to;
20
+ };
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ // If the importer is in node compatibility mode or this is not an ESM
23
+ // file that has been converted to a CommonJS file using a Babel-
24
+ // compatible transform (i.e. "__esModule" has not been set), then set
25
+ // "default" to the CommonJS "module.exports" for node compatibility.
26
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
+ mod
28
+ ));
29
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
+
31
+ // packages/react/toggle-group/src/index.ts
32
+ var src_exports = {};
33
+ __export(src_exports, {
34
+ Item: () => Item2,
35
+ Root: () => Root2,
36
+ ToggleGroup: () => ToggleGroup,
37
+ ToggleGroupItem: () => ToggleGroupItem,
38
+ createToggleGroupScope: () => createToggleGroupScope
39
+ });
40
+ module.exports = __toCommonJS(src_exports);
41
+
42
+ // packages/react/toggle-group/src/ToggleGroup.tsx
43
+ var import_react = __toESM(require("react"));
44
+ var import_react_context = require("@radix-ui/react-context");
45
+ var import_react_primitive = require("@radix-ui/react-primitive");
46
+ var RovingFocusGroup = __toESM(require("@radix-ui/react-roving-focus"));
47
+ var import_react_roving_focus = require("@radix-ui/react-roving-focus");
48
+ var import_react_toggle = require("@radix-ui/react-toggle");
49
+ var import_react_use_controllable_state = require("@radix-ui/react-use-controllable-state");
50
+ var import_react_direction = require("@radix-ui/react-direction");
51
+ var import_jsx_runtime = require("react/jsx-runtime");
52
+ var TOGGLE_GROUP_NAME = "ToggleGroup";
53
+ var [createToggleGroupContext, createToggleGroupScope] = (0, import_react_context.createContextScope)(TOGGLE_GROUP_NAME, [
54
+ import_react_roving_focus.createRovingFocusGroupScope
55
+ ]);
56
+ var useRovingFocusGroupScope = (0, import_react_roving_focus.createRovingFocusGroupScope)();
57
+ var ToggleGroup = import_react.default.forwardRef((props, forwardedRef) => {
58
+ const { type, ...toggleGroupProps } = props;
59
+ if (type === "single") {
60
+ const singleProps = toggleGroupProps;
61
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ToggleGroupImplSingle, { ...singleProps, ref: forwardedRef });
62
+ }
63
+ if (type === "multiple") {
64
+ const multipleProps = toggleGroupProps;
65
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ToggleGroupImplMultiple, { ...multipleProps, ref: forwardedRef });
66
+ }
67
+ throw new Error(`Missing prop \`type\` expected on \`${TOGGLE_GROUP_NAME}\``);
68
+ });
69
+ ToggleGroup.displayName = TOGGLE_GROUP_NAME;
70
+ var [ToggleGroupValueProvider, useToggleGroupValueContext] = createToggleGroupContext(TOGGLE_GROUP_NAME);
71
+ var ToggleGroupImplSingle = import_react.default.forwardRef((props, forwardedRef) => {
72
+ const {
73
+ value: valueProp,
74
+ defaultValue,
75
+ onValueChange = () => {
76
+ },
77
+ ...toggleGroupSingleProps
78
+ } = props;
79
+ const [value, setValue] = (0, import_react_use_controllable_state.useControllableState)({
80
+ prop: valueProp,
81
+ defaultProp: defaultValue,
82
+ onChange: onValueChange
83
+ });
84
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
85
+ ToggleGroupValueProvider,
86
+ {
87
+ scope: props.__scopeToggleGroup,
88
+ type: "single",
89
+ value: value ? [value] : [],
90
+ onItemActivate: setValue,
91
+ onItemDeactivate: import_react.default.useCallback(() => setValue(""), [setValue]),
92
+ children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ToggleGroupImpl, { ...toggleGroupSingleProps, ref: forwardedRef })
93
+ }
94
+ );
95
+ });
96
+ var ToggleGroupImplMultiple = import_react.default.forwardRef((props, forwardedRef) => {
97
+ const {
98
+ value: valueProp,
99
+ defaultValue,
100
+ onValueChange = () => {
101
+ },
102
+ ...toggleGroupMultipleProps
103
+ } = props;
104
+ const [value = [], setValue] = (0, import_react_use_controllable_state.useControllableState)({
105
+ prop: valueProp,
106
+ defaultProp: defaultValue,
107
+ onChange: onValueChange
108
+ });
109
+ const handleButtonActivate = import_react.default.useCallback(
110
+ (itemValue) => setValue((prevValue = []) => [...prevValue, itemValue]),
111
+ [setValue]
112
+ );
113
+ const handleButtonDeactivate = import_react.default.useCallback(
114
+ (itemValue) => setValue((prevValue = []) => prevValue.filter((value2) => value2 !== itemValue)),
115
+ [setValue]
116
+ );
117
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
118
+ ToggleGroupValueProvider,
119
+ {
120
+ scope: props.__scopeToggleGroup,
121
+ type: "multiple",
122
+ value,
123
+ onItemActivate: handleButtonActivate,
124
+ onItemDeactivate: handleButtonDeactivate,
125
+ children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ToggleGroupImpl, { ...toggleGroupMultipleProps, ref: forwardedRef })
126
+ }
127
+ );
128
+ });
129
+ ToggleGroup.displayName = TOGGLE_GROUP_NAME;
130
+ var [ToggleGroupContext, useToggleGroupContext] = createToggleGroupContext(TOGGLE_GROUP_NAME);
131
+ var ToggleGroupImpl = import_react.default.forwardRef(
132
+ (props, forwardedRef) => {
133
+ const {
134
+ __scopeToggleGroup,
135
+ disabled = false,
136
+ rovingFocus = true,
137
+ orientation,
138
+ dir,
139
+ loop = true,
140
+ ...toggleGroupProps
141
+ } = props;
142
+ const rovingFocusGroupScope = useRovingFocusGroupScope(__scopeToggleGroup);
143
+ const direction = (0, import_react_direction.useDirection)(dir);
144
+ const commonProps = { role: "group", dir: direction, ...toggleGroupProps };
145
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ToggleGroupContext, { scope: __scopeToggleGroup, rovingFocus, disabled, children: rovingFocus ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
146
+ RovingFocusGroup.Root,
147
+ {
148
+ asChild: true,
149
+ ...rovingFocusGroupScope,
150
+ orientation,
151
+ dir: direction,
152
+ loop,
153
+ children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_primitive.Primitive.div, { ...commonProps, ref: forwardedRef })
154
+ }
155
+ ) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_primitive.Primitive.div, { ...commonProps, ref: forwardedRef }) });
156
+ }
157
+ );
158
+ var ITEM_NAME = "ToggleGroupItem";
159
+ var ToggleGroupItem = import_react.default.forwardRef(
160
+ (props, forwardedRef) => {
161
+ const valueContext = useToggleGroupValueContext(ITEM_NAME, props.__scopeToggleGroup);
162
+ const context = useToggleGroupContext(ITEM_NAME, props.__scopeToggleGroup);
163
+ const rovingFocusGroupScope = useRovingFocusGroupScope(props.__scopeToggleGroup);
164
+ const pressed = valueContext.value.includes(props.value);
165
+ const disabled = context.disabled || props.disabled;
166
+ const commonProps = { ...props, pressed, disabled };
167
+ const ref = import_react.default.useRef(null);
168
+ return context.rovingFocus ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
169
+ RovingFocusGroup.Item,
170
+ {
171
+ asChild: true,
172
+ ...rovingFocusGroupScope,
173
+ focusable: !disabled,
174
+ active: pressed,
175
+ ref,
176
+ children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ToggleGroupItemImpl, { ...commonProps, ref: forwardedRef })
177
+ }
178
+ ) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ToggleGroupItemImpl, { ...commonProps, ref: forwardedRef });
179
+ }
180
+ );
181
+ ToggleGroupItem.displayName = ITEM_NAME;
182
+ var ToggleGroupItemImpl = import_react.default.forwardRef(
183
+ (props, forwardedRef) => {
184
+ const { __scopeToggleGroup, value, ...itemProps } = props;
185
+ const valueContext = useToggleGroupValueContext(ITEM_NAME, __scopeToggleGroup);
186
+ const singleProps = { role: "radio", "aria-checked": props.pressed, "aria-pressed": void 0 };
187
+ const typeProps = valueContext.type === "single" ? singleProps : void 0;
188
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
189
+ import_react_toggle.Toggle,
190
+ {
191
+ ...typeProps,
192
+ ...itemProps,
193
+ ref: forwardedRef,
194
+ onPressedChange: (pressed) => {
195
+ if (pressed) {
196
+ valueContext.onItemActivate(value);
197
+ } else {
198
+ valueContext.onItemDeactivate(value);
199
+ }
200
+ }
201
+ }
202
+ );
203
+ }
204
+ );
205
+ var Root2 = ToggleGroup;
206
+ var Item2 = ToggleGroupItem;
207
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/index.ts", "../src/ToggleGroup.tsx"],
4
+ "sourcesContent": ["'use client';\nexport {\n createToggleGroupScope,\n //\n ToggleGroup,\n ToggleGroupItem,\n //\n Root,\n Item,\n} from './ToggleGroup';\nexport type {\n ToggleGroupSingleProps,\n ToggleGroupMultipleProps,\n ToggleGroupItemProps,\n} from './ToggleGroup';\n", "import React from 'react';\nimport { createContextScope } from '@radix-ui/react-context';\nimport { Primitive } from '@radix-ui/react-primitive';\nimport * as RovingFocusGroup from '@radix-ui/react-roving-focus';\nimport { createRovingFocusGroupScope } from '@radix-ui/react-roving-focus';\nimport { Toggle } from '@radix-ui/react-toggle';\nimport { useControllableState } from '@radix-ui/react-use-controllable-state';\nimport { useDirection } from '@radix-ui/react-direction';\n\nimport type { Scope } from '@radix-ui/react-context';\n\n/* -------------------------------------------------------------------------------------------------\n * ToggleGroup\n * -----------------------------------------------------------------------------------------------*/\n\nconst TOGGLE_GROUP_NAME = 'ToggleGroup';\n\ntype ScopedProps<P> = P & { __scopeToggleGroup?: Scope };\nconst [createToggleGroupContext, createToggleGroupScope] = createContextScope(TOGGLE_GROUP_NAME, [\n createRovingFocusGroupScope,\n]);\nconst useRovingFocusGroupScope = createRovingFocusGroupScope();\n\ntype ToggleGroupElement = ToggleGroupImplSingleElement | ToggleGroupImplMultipleElement;\ninterface ToggleGroupSingleProps extends ToggleGroupImplSingleProps {\n type: 'single';\n}\ninterface ToggleGroupMultipleProps extends ToggleGroupImplMultipleProps {\n type: 'multiple';\n}\n\nconst ToggleGroup = React.forwardRef<\n ToggleGroupElement,\n ToggleGroupSingleProps | ToggleGroupMultipleProps\n>((props, forwardedRef) => {\n const { type, ...toggleGroupProps } = props;\n\n if (type === 'single') {\n const singleProps = toggleGroupProps as ToggleGroupImplSingleProps;\n return <ToggleGroupImplSingle {...singleProps} ref={forwardedRef} />;\n }\n\n if (type === 'multiple') {\n const multipleProps = toggleGroupProps as ToggleGroupImplMultipleProps;\n return <ToggleGroupImplMultiple {...multipleProps} ref={forwardedRef} />;\n }\n\n throw new Error(`Missing prop \\`type\\` expected on \\`${TOGGLE_GROUP_NAME}\\``);\n});\n\nToggleGroup.displayName = TOGGLE_GROUP_NAME;\n\n/* -----------------------------------------------------------------------------------------------*/\n\ntype ToggleGroupValueContextValue = {\n type: 'single' | 'multiple';\n value: string[];\n onItemActivate(value: string): void;\n onItemDeactivate(value: string): void;\n};\n\nconst [ToggleGroupValueProvider, useToggleGroupValueContext] =\n createToggleGroupContext<ToggleGroupValueContextValue>(TOGGLE_GROUP_NAME);\n\ntype ToggleGroupImplSingleElement = ToggleGroupImplElement;\ninterface ToggleGroupImplSingleProps extends ToggleGroupImplProps {\n /**\n * The controlled stateful value of the item that is pressed.\n */\n value?: string;\n /**\n * The value of the item that is pressed when initially rendered. Use\n * `defaultValue` if you do not need to control the state of a toggle group.\n */\n defaultValue?: string;\n /**\n * The callback that fires when the value of the toggle group changes.\n */\n onValueChange?(value: string): void;\n}\n\nconst ToggleGroupImplSingle = React.forwardRef<\n ToggleGroupImplSingleElement,\n ToggleGroupImplSingleProps\n>((props: ScopedProps<ToggleGroupImplSingleProps>, forwardedRef) => {\n const {\n value: valueProp,\n defaultValue,\n onValueChange = () => {},\n ...toggleGroupSingleProps\n } = props;\n\n const [value, setValue] = useControllableState({\n prop: valueProp,\n defaultProp: defaultValue,\n onChange: onValueChange,\n });\n\n return (\n <ToggleGroupValueProvider\n scope={props.__scopeToggleGroup}\n type=\"single\"\n value={value ? [value] : []}\n onItemActivate={setValue}\n onItemDeactivate={React.useCallback(() => setValue(''), [setValue])}\n >\n <ToggleGroupImpl {...toggleGroupSingleProps} ref={forwardedRef} />\n </ToggleGroupValueProvider>\n );\n});\n\ntype ToggleGroupImplMultipleElement = ToggleGroupImplElement;\ninterface ToggleGroupImplMultipleProps extends ToggleGroupImplProps {\n /**\n * The controlled stateful value of the items that are pressed.\n */\n value?: string[];\n /**\n * The value of the items that are pressed when initially rendered. Use\n * `defaultValue` if you do not need to control the state of a toggle group.\n */\n defaultValue?: string[];\n /**\n * The callback that fires when the state of the toggle group changes.\n */\n onValueChange?(value: string[]): void;\n}\n\nconst ToggleGroupImplMultiple = React.forwardRef<\n ToggleGroupImplMultipleElement,\n ToggleGroupImplMultipleProps\n>((props: ScopedProps<ToggleGroupImplMultipleProps>, forwardedRef) => {\n const {\n value: valueProp,\n defaultValue,\n onValueChange = () => {},\n ...toggleGroupMultipleProps\n } = props;\n\n const [value = [], setValue] = useControllableState({\n prop: valueProp,\n defaultProp: defaultValue,\n onChange: onValueChange,\n });\n\n const handleButtonActivate = React.useCallback(\n (itemValue: string) => setValue((prevValue = []) => [...prevValue, itemValue]),\n [setValue]\n );\n\n const handleButtonDeactivate = React.useCallback(\n (itemValue: string) =>\n setValue((prevValue = []) => prevValue.filter((value) => value !== itemValue)),\n [setValue]\n );\n\n return (\n <ToggleGroupValueProvider\n scope={props.__scopeToggleGroup}\n type=\"multiple\"\n value={value}\n onItemActivate={handleButtonActivate}\n onItemDeactivate={handleButtonDeactivate}\n >\n <ToggleGroupImpl {...toggleGroupMultipleProps} ref={forwardedRef} />\n </ToggleGroupValueProvider>\n );\n});\n\nToggleGroup.displayName = TOGGLE_GROUP_NAME;\n\n/* -----------------------------------------------------------------------------------------------*/\n\ntype ToggleGroupContextValue = { rovingFocus: boolean; disabled: boolean };\n\nconst [ToggleGroupContext, useToggleGroupContext] =\n createToggleGroupContext<ToggleGroupContextValue>(TOGGLE_GROUP_NAME);\n\ntype RovingFocusGroupProps = React.ComponentPropsWithoutRef<typeof RovingFocusGroup.Root>;\ntype ToggleGroupImplElement = React.ElementRef<typeof Primitive.div>;\ntype PrimitiveDivProps = React.ComponentPropsWithoutRef<typeof Primitive.div>;\ninterface ToggleGroupImplProps extends PrimitiveDivProps {\n /**\n * Whether the group is disabled from user interaction.\n * @defaultValue false\n */\n disabled?: boolean;\n /**\n * Whether the group should maintain roving focus of its buttons.\n * @defaultValue true\n */\n rovingFocus?: boolean;\n loop?: RovingFocusGroupProps['loop'];\n orientation?: RovingFocusGroupProps['orientation'];\n dir?: RovingFocusGroupProps['dir'];\n}\n\nconst ToggleGroupImpl = React.forwardRef<ToggleGroupImplElement, ToggleGroupImplProps>(\n (props: ScopedProps<ToggleGroupImplProps>, forwardedRef) => {\n const {\n __scopeToggleGroup,\n disabled = false,\n rovingFocus = true,\n orientation,\n dir,\n loop = true,\n ...toggleGroupProps\n } = props;\n const rovingFocusGroupScope = useRovingFocusGroupScope(__scopeToggleGroup);\n const direction = useDirection(dir);\n const commonProps = { role: 'group', dir: direction, ...toggleGroupProps };\n return (\n <ToggleGroupContext scope={__scopeToggleGroup} rovingFocus={rovingFocus} disabled={disabled}>\n {rovingFocus ? (\n <RovingFocusGroup.Root\n asChild\n {...rovingFocusGroupScope}\n orientation={orientation}\n dir={direction}\n loop={loop}\n >\n <Primitive.div {...commonProps} ref={forwardedRef} />\n </RovingFocusGroup.Root>\n ) : (\n <Primitive.div {...commonProps} ref={forwardedRef} />\n )}\n </ToggleGroupContext>\n );\n }\n);\n\n/* -------------------------------------------------------------------------------------------------\n * ToggleGroupItem\n * -----------------------------------------------------------------------------------------------*/\n\nconst ITEM_NAME = 'ToggleGroupItem';\n\ntype ToggleGroupItemElement = ToggleGroupItemImplElement;\ninterface ToggleGroupItemProps extends Omit<ToggleGroupItemImplProps, 'pressed'> {}\n\nconst ToggleGroupItem = React.forwardRef<ToggleGroupItemElement, ToggleGroupItemProps>(\n (props: ScopedProps<ToggleGroupItemProps>, forwardedRef) => {\n const valueContext = useToggleGroupValueContext(ITEM_NAME, props.__scopeToggleGroup);\n const context = useToggleGroupContext(ITEM_NAME, props.__scopeToggleGroup);\n const rovingFocusGroupScope = useRovingFocusGroupScope(props.__scopeToggleGroup);\n const pressed = valueContext.value.includes(props.value);\n const disabled = context.disabled || props.disabled;\n const commonProps = { ...props, pressed, disabled };\n const ref = React.useRef<HTMLDivElement>(null);\n return context.rovingFocus ? (\n <RovingFocusGroup.Item\n asChild\n {...rovingFocusGroupScope}\n focusable={!disabled}\n active={pressed}\n ref={ref}\n >\n <ToggleGroupItemImpl {...commonProps} ref={forwardedRef} />\n </RovingFocusGroup.Item>\n ) : (\n <ToggleGroupItemImpl {...commonProps} ref={forwardedRef} />\n );\n }\n);\n\nToggleGroupItem.displayName = ITEM_NAME;\n\n/* -----------------------------------------------------------------------------------------------*/\n\ntype ToggleGroupItemImplElement = React.ElementRef<typeof Toggle>;\ntype ToggleProps = React.ComponentPropsWithoutRef<typeof Toggle>;\ninterface ToggleGroupItemImplProps extends Omit<ToggleProps, 'defaultPressed' | 'onPressedChange'> {\n /**\n * A string value for the toggle group item. All items within a toggle group should use a unique value.\n */\n value: string;\n}\n\nconst ToggleGroupItemImpl = React.forwardRef<ToggleGroupItemImplElement, ToggleGroupItemImplProps>(\n (props: ScopedProps<ToggleGroupItemImplProps>, forwardedRef) => {\n const { __scopeToggleGroup, value, ...itemProps } = props;\n const valueContext = useToggleGroupValueContext(ITEM_NAME, __scopeToggleGroup);\n const singleProps = { role: 'radio', 'aria-checked': props.pressed, 'aria-pressed': undefined };\n const typeProps = valueContext.type === 'single' ? singleProps : undefined;\n return (\n <Toggle\n {...typeProps}\n {...itemProps}\n ref={forwardedRef}\n onPressedChange={(pressed) => {\n if (pressed) {\n valueContext.onItemActivate(value);\n } else {\n valueContext.onItemDeactivate(value);\n }\n }}\n />\n );\n }\n);\n\n/* -----------------------------------------------------------------------------------------------*/\n\nconst Root = ToggleGroup;\nconst Item = ToggleGroupItem;\n\nexport {\n createToggleGroupScope,\n //\n ToggleGroup,\n ToggleGroupItem,\n //\n Root,\n Item,\n};\nexport type { ToggleGroupSingleProps, ToggleGroupMultipleProps, ToggleGroupItemProps };\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA,cAAAA;AAAA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,iBAAiB;AAC9D,UAAM,EAAE,oBAAoB,OAAO,GAAG,UAAU,IAAI;AACpD,UAAM,eAAe,2BAA2B,WAAW,kBAAkB;AAC7E,UAAM,cAAc,EAAE,MAAM,SAAS,gBAAgB,MAAM,SAAS,gBAAgB,OAAU;AAC9F,UAAM,YAAY,aAAa,SAAS,WAAW,cAAc;AACjE,WACE;AAAA,MAAC;AAAA;AAAA,QACE,GAAG;AAAA,QACH,GAAG;AAAA,QACJ,KAAK;AAAA,QACL,iBAAiB,CAAC,YAAY;AAC5B,cAAI,SAAS;AACX,yBAAa,eAAe,KAAK;AAAA,UACnC,OAAO;AACL,yBAAa,iBAAiB,KAAK;AAAA,UACrC;AAAA,QACF;AAAA;AAAA,IACF;AAAA,EAEJ;AACF;AAIA,IAAME,QAAO;AACb,IAAMC,QAAO;",
6
+ "names": ["Item", "Root", "React", "value", "Root", "Item"]
7
+ }
package/dist/index.mjs ADDED
@@ -0,0 +1,175 @@
1
+ "use client";
2
+
3
+ // packages/react/toggle-group/src/ToggleGroup.tsx
4
+ import React from "react";
5
+ import { createContextScope } from "@radix-ui/react-context";
6
+ import { Primitive } from "@radix-ui/react-primitive";
7
+ import * as RovingFocusGroup from "@radix-ui/react-roving-focus";
8
+ import { createRovingFocusGroupScope } from "@radix-ui/react-roving-focus";
9
+ import { Toggle } from "@radix-ui/react-toggle";
10
+ import { useControllableState } from "@radix-ui/react-use-controllable-state";
11
+ import { useDirection } from "@radix-ui/react-direction";
12
+ import { jsx } from "react/jsx-runtime";
13
+ var TOGGLE_GROUP_NAME = "ToggleGroup";
14
+ var [createToggleGroupContext, createToggleGroupScope] = createContextScope(TOGGLE_GROUP_NAME, [
15
+ createRovingFocusGroupScope
16
+ ]);
17
+ var useRovingFocusGroupScope = createRovingFocusGroupScope();
18
+ var ToggleGroup = React.forwardRef((props, forwardedRef) => {
19
+ const { type, ...toggleGroupProps } = props;
20
+ if (type === "single") {
21
+ const singleProps = toggleGroupProps;
22
+ return /* @__PURE__ */ jsx(ToggleGroupImplSingle, { ...singleProps, ref: forwardedRef });
23
+ }
24
+ if (type === "multiple") {
25
+ const multipleProps = toggleGroupProps;
26
+ return /* @__PURE__ */ jsx(ToggleGroupImplMultiple, { ...multipleProps, ref: forwardedRef });
27
+ }
28
+ throw new Error(`Missing prop \`type\` expected on \`${TOGGLE_GROUP_NAME}\``);
29
+ });
30
+ ToggleGroup.displayName = TOGGLE_GROUP_NAME;
31
+ var [ToggleGroupValueProvider, useToggleGroupValueContext] = createToggleGroupContext(TOGGLE_GROUP_NAME);
32
+ var ToggleGroupImplSingle = React.forwardRef((props, forwardedRef) => {
33
+ const {
34
+ value: valueProp,
35
+ defaultValue,
36
+ onValueChange = () => {
37
+ },
38
+ ...toggleGroupSingleProps
39
+ } = props;
40
+ const [value, setValue] = useControllableState({
41
+ prop: valueProp,
42
+ defaultProp: defaultValue,
43
+ onChange: onValueChange
44
+ });
45
+ return /* @__PURE__ */ jsx(
46
+ ToggleGroupValueProvider,
47
+ {
48
+ scope: props.__scopeToggleGroup,
49
+ type: "single",
50
+ value: value ? [value] : [],
51
+ onItemActivate: setValue,
52
+ onItemDeactivate: React.useCallback(() => setValue(""), [setValue]),
53
+ children: /* @__PURE__ */ jsx(ToggleGroupImpl, { ...toggleGroupSingleProps, ref: forwardedRef })
54
+ }
55
+ );
56
+ });
57
+ var ToggleGroupImplMultiple = React.forwardRef((props, forwardedRef) => {
58
+ const {
59
+ value: valueProp,
60
+ defaultValue,
61
+ onValueChange = () => {
62
+ },
63
+ ...toggleGroupMultipleProps
64
+ } = props;
65
+ const [value = [], setValue] = useControllableState({
66
+ prop: valueProp,
67
+ defaultProp: defaultValue,
68
+ onChange: onValueChange
69
+ });
70
+ const handleButtonActivate = React.useCallback(
71
+ (itemValue) => setValue((prevValue = []) => [...prevValue, itemValue]),
72
+ [setValue]
73
+ );
74
+ const handleButtonDeactivate = React.useCallback(
75
+ (itemValue) => setValue((prevValue = []) => prevValue.filter((value2) => value2 !== itemValue)),
76
+ [setValue]
77
+ );
78
+ return /* @__PURE__ */ jsx(
79
+ ToggleGroupValueProvider,
80
+ {
81
+ scope: props.__scopeToggleGroup,
82
+ type: "multiple",
83
+ value,
84
+ onItemActivate: handleButtonActivate,
85
+ onItemDeactivate: handleButtonDeactivate,
86
+ children: /* @__PURE__ */ jsx(ToggleGroupImpl, { ...toggleGroupMultipleProps, ref: forwardedRef })
87
+ }
88
+ );
89
+ });
90
+ ToggleGroup.displayName = TOGGLE_GROUP_NAME;
91
+ var [ToggleGroupContext, useToggleGroupContext] = createToggleGroupContext(TOGGLE_GROUP_NAME);
92
+ var ToggleGroupImpl = React.forwardRef(
93
+ (props, forwardedRef) => {
94
+ const {
95
+ __scopeToggleGroup,
96
+ disabled = false,
97
+ rovingFocus = true,
98
+ orientation,
99
+ dir,
100
+ loop = true,
101
+ ...toggleGroupProps
102
+ } = props;
103
+ const rovingFocusGroupScope = useRovingFocusGroupScope(__scopeToggleGroup);
104
+ const direction = useDirection(dir);
105
+ const commonProps = { role: "group", dir: direction, ...toggleGroupProps };
106
+ return /* @__PURE__ */ jsx(ToggleGroupContext, { scope: __scopeToggleGroup, rovingFocus, disabled, children: rovingFocus ? /* @__PURE__ */ jsx(
107
+ RovingFocusGroup.Root,
108
+ {
109
+ asChild: true,
110
+ ...rovingFocusGroupScope,
111
+ orientation,
112
+ dir: direction,
113
+ loop,
114
+ children: /* @__PURE__ */ jsx(Primitive.div, { ...commonProps, ref: forwardedRef })
115
+ }
116
+ ) : /* @__PURE__ */ jsx(Primitive.div, { ...commonProps, ref: forwardedRef }) });
117
+ }
118
+ );
119
+ var ITEM_NAME = "ToggleGroupItem";
120
+ var ToggleGroupItem = React.forwardRef(
121
+ (props, forwardedRef) => {
122
+ const valueContext = useToggleGroupValueContext(ITEM_NAME, props.__scopeToggleGroup);
123
+ const context = useToggleGroupContext(ITEM_NAME, props.__scopeToggleGroup);
124
+ const rovingFocusGroupScope = useRovingFocusGroupScope(props.__scopeToggleGroup);
125
+ const pressed = valueContext.value.includes(props.value);
126
+ const disabled = context.disabled || props.disabled;
127
+ const commonProps = { ...props, pressed, disabled };
128
+ const ref = React.useRef(null);
129
+ return context.rovingFocus ? /* @__PURE__ */ jsx(
130
+ RovingFocusGroup.Item,
131
+ {
132
+ asChild: true,
133
+ ...rovingFocusGroupScope,
134
+ focusable: !disabled,
135
+ active: pressed,
136
+ ref,
137
+ children: /* @__PURE__ */ jsx(ToggleGroupItemImpl, { ...commonProps, ref: forwardedRef })
138
+ }
139
+ ) : /* @__PURE__ */ jsx(ToggleGroupItemImpl, { ...commonProps, ref: forwardedRef });
140
+ }
141
+ );
142
+ ToggleGroupItem.displayName = ITEM_NAME;
143
+ var ToggleGroupItemImpl = React.forwardRef(
144
+ (props, forwardedRef) => {
145
+ const { __scopeToggleGroup, value, ...itemProps } = props;
146
+ const valueContext = useToggleGroupValueContext(ITEM_NAME, __scopeToggleGroup);
147
+ const singleProps = { role: "radio", "aria-checked": props.pressed, "aria-pressed": void 0 };
148
+ const typeProps = valueContext.type === "single" ? singleProps : void 0;
149
+ return /* @__PURE__ */ jsx(
150
+ Toggle,
151
+ {
152
+ ...typeProps,
153
+ ...itemProps,
154
+ ref: forwardedRef,
155
+ onPressedChange: (pressed) => {
156
+ if (pressed) {
157
+ valueContext.onItemActivate(value);
158
+ } else {
159
+ valueContext.onItemDeactivate(value);
160
+ }
161
+ }
162
+ }
163
+ );
164
+ }
165
+ );
166
+ var Root2 = ToggleGroup;
167
+ var Item2 = ToggleGroupItem;
168
+ export {
169
+ Item2 as Item,
170
+ Root2 as Root,
171
+ ToggleGroup,
172
+ ToggleGroupItem,
173
+ createToggleGroupScope
174
+ };
175
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/ToggleGroup.tsx"],
4
+ "sourcesContent": ["import React from 'react';\nimport { createContextScope } from '@radix-ui/react-context';\nimport { Primitive } from '@radix-ui/react-primitive';\nimport * as RovingFocusGroup from '@radix-ui/react-roving-focus';\nimport { createRovingFocusGroupScope } from '@radix-ui/react-roving-focus';\nimport { Toggle } from '@radix-ui/react-toggle';\nimport { useControllableState } from '@radix-ui/react-use-controllable-state';\nimport { useDirection } from '@radix-ui/react-direction';\n\nimport type { Scope } from '@radix-ui/react-context';\n\n/* -------------------------------------------------------------------------------------------------\n * ToggleGroup\n * -----------------------------------------------------------------------------------------------*/\n\nconst TOGGLE_GROUP_NAME = 'ToggleGroup';\n\ntype ScopedProps<P> = P & { __scopeToggleGroup?: Scope };\nconst [createToggleGroupContext, createToggleGroupScope] = createContextScope(TOGGLE_GROUP_NAME, [\n createRovingFocusGroupScope,\n]);\nconst useRovingFocusGroupScope = createRovingFocusGroupScope();\n\ntype ToggleGroupElement = ToggleGroupImplSingleElement | ToggleGroupImplMultipleElement;\ninterface ToggleGroupSingleProps extends ToggleGroupImplSingleProps {\n type: 'single';\n}\ninterface ToggleGroupMultipleProps extends ToggleGroupImplMultipleProps {\n type: 'multiple';\n}\n\nconst ToggleGroup = React.forwardRef<\n ToggleGroupElement,\n ToggleGroupSingleProps | ToggleGroupMultipleProps\n>((props, forwardedRef) => {\n const { type, ...toggleGroupProps } = props;\n\n if (type === 'single') {\n const singleProps = toggleGroupProps as ToggleGroupImplSingleProps;\n return <ToggleGroupImplSingle {...singleProps} ref={forwardedRef} />;\n }\n\n if (type === 'multiple') {\n const multipleProps = toggleGroupProps as ToggleGroupImplMultipleProps;\n return <ToggleGroupImplMultiple {...multipleProps} ref={forwardedRef} />;\n }\n\n throw new Error(`Missing prop \\`type\\` expected on \\`${TOGGLE_GROUP_NAME}\\``);\n});\n\nToggleGroup.displayName = TOGGLE_GROUP_NAME;\n\n/* -----------------------------------------------------------------------------------------------*/\n\ntype ToggleGroupValueContextValue = {\n type: 'single' | 'multiple';\n value: string[];\n onItemActivate(value: string): void;\n onItemDeactivate(value: string): void;\n};\n\nconst [ToggleGroupValueProvider, useToggleGroupValueContext] =\n createToggleGroupContext<ToggleGroupValueContextValue>(TOGGLE_GROUP_NAME);\n\ntype ToggleGroupImplSingleElement = ToggleGroupImplElement;\ninterface ToggleGroupImplSingleProps extends ToggleGroupImplProps {\n /**\n * The controlled stateful value of the item that is pressed.\n */\n value?: string;\n /**\n * The value of the item that is pressed when initially rendered. Use\n * `defaultValue` if you do not need to control the state of a toggle group.\n */\n defaultValue?: string;\n /**\n * The callback that fires when the value of the toggle group changes.\n */\n onValueChange?(value: string): void;\n}\n\nconst ToggleGroupImplSingle = React.forwardRef<\n ToggleGroupImplSingleElement,\n ToggleGroupImplSingleProps\n>((props: ScopedProps<ToggleGroupImplSingleProps>, forwardedRef) => {\n const {\n value: valueProp,\n defaultValue,\n onValueChange = () => {},\n ...toggleGroupSingleProps\n } = props;\n\n const [value, setValue] = useControllableState({\n prop: valueProp,\n defaultProp: defaultValue,\n onChange: onValueChange,\n });\n\n return (\n <ToggleGroupValueProvider\n scope={props.__scopeToggleGroup}\n type=\"single\"\n value={value ? [value] : []}\n onItemActivate={setValue}\n onItemDeactivate={React.useCallback(() => setValue(''), [setValue])}\n >\n <ToggleGroupImpl {...toggleGroupSingleProps} ref={forwardedRef} />\n </ToggleGroupValueProvider>\n );\n});\n\ntype ToggleGroupImplMultipleElement = ToggleGroupImplElement;\ninterface ToggleGroupImplMultipleProps extends ToggleGroupImplProps {\n /**\n * The controlled stateful value of the items that are pressed.\n */\n value?: string[];\n /**\n * The value of the items that are pressed when initially rendered. Use\n * `defaultValue` if you do not need to control the state of a toggle group.\n */\n defaultValue?: string[];\n /**\n * The callback that fires when the state of the toggle group changes.\n */\n onValueChange?(value: string[]): void;\n}\n\nconst ToggleGroupImplMultiple = React.forwardRef<\n ToggleGroupImplMultipleElement,\n ToggleGroupImplMultipleProps\n>((props: ScopedProps<ToggleGroupImplMultipleProps>, forwardedRef) => {\n const {\n value: valueProp,\n defaultValue,\n onValueChange = () => {},\n ...toggleGroupMultipleProps\n } = props;\n\n const [value = [], setValue] = useControllableState({\n prop: valueProp,\n defaultProp: defaultValue,\n onChange: onValueChange,\n });\n\n const handleButtonActivate = React.useCallback(\n (itemValue: string) => setValue((prevValue = []) => [...prevValue, itemValue]),\n [setValue]\n );\n\n const handleButtonDeactivate = React.useCallback(\n (itemValue: string) =>\n setValue((prevValue = []) => prevValue.filter((value) => value !== itemValue)),\n [setValue]\n );\n\n return (\n <ToggleGroupValueProvider\n scope={props.__scopeToggleGroup}\n type=\"multiple\"\n value={value}\n onItemActivate={handleButtonActivate}\n onItemDeactivate={handleButtonDeactivate}\n >\n <ToggleGroupImpl {...toggleGroupMultipleProps} ref={forwardedRef} />\n </ToggleGroupValueProvider>\n );\n});\n\nToggleGroup.displayName = TOGGLE_GROUP_NAME;\n\n/* -----------------------------------------------------------------------------------------------*/\n\ntype ToggleGroupContextValue = { rovingFocus: boolean; disabled: boolean };\n\nconst [ToggleGroupContext, useToggleGroupContext] =\n createToggleGroupContext<ToggleGroupContextValue>(TOGGLE_GROUP_NAME);\n\ntype RovingFocusGroupProps = React.ComponentPropsWithoutRef<typeof RovingFocusGroup.Root>;\ntype ToggleGroupImplElement = React.ElementRef<typeof Primitive.div>;\ntype PrimitiveDivProps = React.ComponentPropsWithoutRef<typeof Primitive.div>;\ninterface ToggleGroupImplProps extends PrimitiveDivProps {\n /**\n * Whether the group is disabled from user interaction.\n * @defaultValue false\n */\n disabled?: boolean;\n /**\n * Whether the group should maintain roving focus of its buttons.\n * @defaultValue true\n */\n rovingFocus?: boolean;\n loop?: RovingFocusGroupProps['loop'];\n orientation?: RovingFocusGroupProps['orientation'];\n dir?: RovingFocusGroupProps['dir'];\n}\n\nconst ToggleGroupImpl = React.forwardRef<ToggleGroupImplElement, ToggleGroupImplProps>(\n (props: ScopedProps<ToggleGroupImplProps>, forwardedRef) => {\n const {\n __scopeToggleGroup,\n disabled = false,\n rovingFocus = true,\n orientation,\n dir,\n loop = true,\n ...toggleGroupProps\n } = props;\n const rovingFocusGroupScope = useRovingFocusGroupScope(__scopeToggleGroup);\n const direction = useDirection(dir);\n const commonProps = { role: 'group', dir: direction, ...toggleGroupProps };\n return (\n <ToggleGroupContext scope={__scopeToggleGroup} rovingFocus={rovingFocus} disabled={disabled}>\n {rovingFocus ? (\n <RovingFocusGroup.Root\n asChild\n {...rovingFocusGroupScope}\n orientation={orientation}\n dir={direction}\n loop={loop}\n >\n <Primitive.div {...commonProps} ref={forwardedRef} />\n </RovingFocusGroup.Root>\n ) : (\n <Primitive.div {...commonProps} ref={forwardedRef} />\n )}\n </ToggleGroupContext>\n );\n }\n);\n\n/* -------------------------------------------------------------------------------------------------\n * ToggleGroupItem\n * -----------------------------------------------------------------------------------------------*/\n\nconst ITEM_NAME = 'ToggleGroupItem';\n\ntype ToggleGroupItemElement = ToggleGroupItemImplElement;\ninterface ToggleGroupItemProps extends Omit<ToggleGroupItemImplProps, 'pressed'> {}\n\nconst ToggleGroupItem = React.forwardRef<ToggleGroupItemElement, ToggleGroupItemProps>(\n (props: ScopedProps<ToggleGroupItemProps>, forwardedRef) => {\n const valueContext = useToggleGroupValueContext(ITEM_NAME, props.__scopeToggleGroup);\n const context = useToggleGroupContext(ITEM_NAME, props.__scopeToggleGroup);\n const rovingFocusGroupScope = useRovingFocusGroupScope(props.__scopeToggleGroup);\n const pressed = valueContext.value.includes(props.value);\n const disabled = context.disabled || props.disabled;\n const commonProps = { ...props, pressed, disabled };\n const ref = React.useRef<HTMLDivElement>(null);\n return context.rovingFocus ? (\n <RovingFocusGroup.Item\n asChild\n {...rovingFocusGroupScope}\n focusable={!disabled}\n active={pressed}\n ref={ref}\n >\n <ToggleGroupItemImpl {...commonProps} ref={forwardedRef} />\n </RovingFocusGroup.Item>\n ) : (\n <ToggleGroupItemImpl {...commonProps} ref={forwardedRef} />\n );\n }\n);\n\nToggleGroupItem.displayName = ITEM_NAME;\n\n/* -----------------------------------------------------------------------------------------------*/\n\ntype ToggleGroupItemImplElement = React.ElementRef<typeof Toggle>;\ntype ToggleProps = React.ComponentPropsWithoutRef<typeof Toggle>;\ninterface ToggleGroupItemImplProps extends Omit<ToggleProps, 'defaultPressed' | 'onPressedChange'> {\n /**\n * A string value for the toggle group item. All items within a toggle group should use a unique value.\n */\n value: string;\n}\n\nconst ToggleGroupItemImpl = React.forwardRef<ToggleGroupItemImplElement, ToggleGroupItemImplProps>(\n (props: ScopedProps<ToggleGroupItemImplProps>, forwardedRef) => {\n const { __scopeToggleGroup, value, ...itemProps } = props;\n const valueContext = useToggleGroupValueContext(ITEM_NAME, __scopeToggleGroup);\n const singleProps = { role: 'radio', 'aria-checked': props.pressed, 'aria-pressed': undefined };\n const typeProps = valueContext.type === 'single' ? singleProps : undefined;\n return (\n <Toggle\n {...typeProps}\n {...itemProps}\n ref={forwardedRef}\n onPressedChange={(pressed) => {\n if (pressed) {\n valueContext.onItemActivate(value);\n } else {\n valueContext.onItemDeactivate(value);\n }\n }}\n />\n );\n }\n);\n\n/* -----------------------------------------------------------------------------------------------*/\n\nconst Root = ToggleGroup;\nconst Item = ToggleGroupItem;\n\nexport {\n createToggleGroupScope,\n //\n ToggleGroup,\n ToggleGroupItem,\n //\n Root,\n Item,\n};\nexport type { ToggleGroupSingleProps, ToggleGroupMultipleProps, ToggleGroupItemProps };\n"],
5
+ "mappings": ";;;AAAA,OAAO,WAAW;AAClB,SAAS,0BAA0B;AACnC,SAAS,iBAAiB;AAC1B,YAAY,sBAAsB;AAClC,SAAS,mCAAmC;AAC5C,SAAS,cAAc;AACvB,SAAS,4BAA4B;AACrC,SAAS,oBAAoB;AAgClB;AAxBX,IAAM,oBAAoB;AAG1B,IAAM,CAAC,0BAA0B,sBAAsB,IAAI,mBAAmB,mBAAmB;AAAA,EAC/F;AACF,CAAC;AACD,IAAM,2BAA2B,4BAA4B;AAU7D,IAAM,cAAc,MAAM,WAGxB,CAAC,OAAO,iBAAiB;AACzB,QAAM,EAAE,MAAM,GAAG,iBAAiB,IAAI;AAEtC,MAAI,SAAS,UAAU;AACrB,UAAM,cAAc;AACpB,WAAO,oBAAC,yBAAu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ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@radix-ui/react-toggle-group",
3
+ "version": "0.0.0-20250116175529",
4
+ "license": "MIT",
5
+ "exports": {
6
+ ".": {
7
+ "import": {
8
+ "types": "./dist/index.d.mts",
9
+ "default": "./dist/index.mjs"
10
+ },
11
+ "require": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.js"
14
+ }
15
+ }
16
+ },
17
+ "source": "./src/index.ts",
18
+ "main": "./dist/index.js",
19
+ "module": "./dist/index.mjs",
20
+ "types": "./dist/index.d.ts",
21
+ "files": [
22
+ "dist",
23
+ "README.md"
24
+ ],
25
+ "sideEffects": false,
26
+ "scripts": {
27
+ "clean": "rm -rf dist"
28
+ },
29
+ "dependencies": {
30
+ "@radix-ui/primitive": "workspace:*",
31
+ "@radix-ui/react-context": "workspace:*",
32
+ "@radix-ui/react-direction": "workspace:*",
33
+ "@radix-ui/react-primitive": "workspace:*",
34
+ "@radix-ui/react-roving-focus": "workspace:*",
35
+ "@radix-ui/react-toggle": "workspace:*",
36
+ "@radix-ui/react-use-controllable-state": "workspace:*"
37
+ },
38
+ "peerDependencies": {
39
+ "@types/react": "*",
40
+ "@types/react-dom": "*",
41
+ "react": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc",
42
+ "react-dom": "^16.8 || ^17.0 || ^18.0 || ^19.0 || ^19.0.0-rc"
43
+ },
44
+ "peerDependenciesMeta": {
45
+ "@types/react": {
46
+ "optional": true
47
+ },
48
+ "@types/react-dom": {
49
+ "optional": true
50
+ }
51
+ },
52
+ "homepage": "https://radix-ui.com/primitives",
53
+ "repository": {
54
+ "type": "git",
55
+ "url": "git+https://github.com/radix-ui/primitives.git"
56
+ },
57
+ "bugs": {
58
+ "url": "https://github.com/radix-ui/primitives/issues"
59
+ }
60
+ }