@glob-ui/toggle 0.0.0-stage → 0.2.0
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 +16 -2
- package/dist/index.d.mts +45 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.js +228 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +201 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +66 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kevin Martínez E
|
|
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
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @glob-ui/toggle
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A button with a pressed state, and a group of them with single or multiple selection.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @glob-ui/toggle
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Documentation
|
|
12
|
+
|
|
13
|
+
Full docs, examples and the accessibility table: [Toggle](https://github.com/kevin-martineze/GlobUI/tree/main/apps/docs/content/docs/components/toggle.mdx).
|
|
14
|
+
|
|
15
|
+
## License
|
|
16
|
+
|
|
17
|
+
MIT. See [LICENSE](./LICENSE).
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import * as class_variance_authority_types from 'class-variance-authority/types';
|
|
3
|
+
import { VariantProps } from 'class-variance-authority';
|
|
4
|
+
|
|
5
|
+
declare const toggleVariants: (props?: ({
|
|
6
|
+
variant?: "default" | "outline" | null | undefined;
|
|
7
|
+
size?: "sm" | "md" | "lg" | null | undefined;
|
|
8
|
+
} & class_variance_authority_types.ClassProp) | undefined) => string;
|
|
9
|
+
interface ToggleProps extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, "value">, VariantProps<typeof toggleVariants> {
|
|
10
|
+
pressed?: boolean;
|
|
11
|
+
defaultPressed?: boolean;
|
|
12
|
+
onPressedChange?: (pressed: boolean) => void;
|
|
13
|
+
}
|
|
14
|
+
/** A button with an on and off state (`aria-pressed`). Give it an accessible name when it has only an icon. */
|
|
15
|
+
declare const Toggle: react.ForwardRefExoticComponent<ToggleProps & react.RefAttributes<HTMLButtonElement>>;
|
|
16
|
+
type CommonGroupProps = Omit<React.HTMLAttributes<HTMLDivElement>, "defaultValue" | "onChange"> & VariantProps<typeof toggleVariants> & {
|
|
17
|
+
orientation?: "horizontal" | "vertical";
|
|
18
|
+
disabled?: boolean;
|
|
19
|
+
};
|
|
20
|
+
type ToggleGroupSingleProps = CommonGroupProps & {
|
|
21
|
+
type: "single";
|
|
22
|
+
/** The pressed item's value, or "" when none is pressed. */
|
|
23
|
+
value?: string;
|
|
24
|
+
defaultValue?: string;
|
|
25
|
+
onValueChange?: (value: string) => void;
|
|
26
|
+
};
|
|
27
|
+
type ToggleGroupMultipleProps = CommonGroupProps & {
|
|
28
|
+
type: "multiple";
|
|
29
|
+
value?: string[];
|
|
30
|
+
defaultValue?: string[];
|
|
31
|
+
onValueChange?: (value: string[]) => void;
|
|
32
|
+
};
|
|
33
|
+
type ToggleGroupProps = ToggleGroupSingleProps | ToggleGroupMultipleProps;
|
|
34
|
+
/**
|
|
35
|
+
* A row of toggles. `single` lets one item be pressed (pressing it again clears the choice);
|
|
36
|
+
* `multiple` lets several be pressed. Items are `aria-pressed` buttons in one tab stop: arrows move focus.
|
|
37
|
+
*/
|
|
38
|
+
declare const ToggleGroup: react.ForwardRefExoticComponent<ToggleGroupProps & react.RefAttributes<HTMLDivElement>>;
|
|
39
|
+
interface ToggleGroupItemProps extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, "value"> {
|
|
40
|
+
/** The value this item stands for in the group's `value`. */
|
|
41
|
+
value: string;
|
|
42
|
+
}
|
|
43
|
+
declare const ToggleGroupItem: react.ForwardRefExoticComponent<ToggleGroupItemProps & react.RefAttributes<HTMLButtonElement>>;
|
|
44
|
+
|
|
45
|
+
export { Toggle, ToggleGroup, ToggleGroupItem, type ToggleGroupItemProps, type ToggleGroupMultipleProps, type ToggleGroupProps, type ToggleGroupSingleProps, type ToggleProps, toggleVariants };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import * as class_variance_authority_types from 'class-variance-authority/types';
|
|
3
|
+
import { VariantProps } from 'class-variance-authority';
|
|
4
|
+
|
|
5
|
+
declare const toggleVariants: (props?: ({
|
|
6
|
+
variant?: "default" | "outline" | null | undefined;
|
|
7
|
+
size?: "sm" | "md" | "lg" | null | undefined;
|
|
8
|
+
} & class_variance_authority_types.ClassProp) | undefined) => string;
|
|
9
|
+
interface ToggleProps extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, "value">, VariantProps<typeof toggleVariants> {
|
|
10
|
+
pressed?: boolean;
|
|
11
|
+
defaultPressed?: boolean;
|
|
12
|
+
onPressedChange?: (pressed: boolean) => void;
|
|
13
|
+
}
|
|
14
|
+
/** A button with an on and off state (`aria-pressed`). Give it an accessible name when it has only an icon. */
|
|
15
|
+
declare const Toggle: react.ForwardRefExoticComponent<ToggleProps & react.RefAttributes<HTMLButtonElement>>;
|
|
16
|
+
type CommonGroupProps = Omit<React.HTMLAttributes<HTMLDivElement>, "defaultValue" | "onChange"> & VariantProps<typeof toggleVariants> & {
|
|
17
|
+
orientation?: "horizontal" | "vertical";
|
|
18
|
+
disabled?: boolean;
|
|
19
|
+
};
|
|
20
|
+
type ToggleGroupSingleProps = CommonGroupProps & {
|
|
21
|
+
type: "single";
|
|
22
|
+
/** The pressed item's value, or "" when none is pressed. */
|
|
23
|
+
value?: string;
|
|
24
|
+
defaultValue?: string;
|
|
25
|
+
onValueChange?: (value: string) => void;
|
|
26
|
+
};
|
|
27
|
+
type ToggleGroupMultipleProps = CommonGroupProps & {
|
|
28
|
+
type: "multiple";
|
|
29
|
+
value?: string[];
|
|
30
|
+
defaultValue?: string[];
|
|
31
|
+
onValueChange?: (value: string[]) => void;
|
|
32
|
+
};
|
|
33
|
+
type ToggleGroupProps = ToggleGroupSingleProps | ToggleGroupMultipleProps;
|
|
34
|
+
/**
|
|
35
|
+
* A row of toggles. `single` lets one item be pressed (pressing it again clears the choice);
|
|
36
|
+
* `multiple` lets several be pressed. Items are `aria-pressed` buttons in one tab stop: arrows move focus.
|
|
37
|
+
*/
|
|
38
|
+
declare const ToggleGroup: react.ForwardRefExoticComponent<ToggleGroupProps & react.RefAttributes<HTMLDivElement>>;
|
|
39
|
+
interface ToggleGroupItemProps extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, "value"> {
|
|
40
|
+
/** The value this item stands for in the group's `value`. */
|
|
41
|
+
value: string;
|
|
42
|
+
}
|
|
43
|
+
declare const ToggleGroupItem: react.ForwardRefExoticComponent<ToggleGroupItemProps & react.RefAttributes<HTMLButtonElement>>;
|
|
44
|
+
|
|
45
|
+
export { Toggle, ToggleGroup, ToggleGroupItem, type ToggleGroupItemProps, type ToggleGroupMultipleProps, type ToggleGroupProps, type ToggleGroupSingleProps, type ToggleProps, toggleVariants };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __defProps = Object.defineProperties;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
7
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
9
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
11
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
12
|
+
var __spreadValues = (a, b) => {
|
|
13
|
+
for (var prop in b || (b = {}))
|
|
14
|
+
if (__hasOwnProp.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
if (__getOwnPropSymbols)
|
|
17
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
18
|
+
if (__propIsEnum.call(b, prop))
|
|
19
|
+
__defNormalProp(a, prop, b[prop]);
|
|
20
|
+
}
|
|
21
|
+
return a;
|
|
22
|
+
};
|
|
23
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
24
|
+
var __objRest = (source, exclude) => {
|
|
25
|
+
var target = {};
|
|
26
|
+
for (var prop in source)
|
|
27
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
28
|
+
target[prop] = source[prop];
|
|
29
|
+
if (source != null && __getOwnPropSymbols)
|
|
30
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
31
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
32
|
+
target[prop] = source[prop];
|
|
33
|
+
}
|
|
34
|
+
return target;
|
|
35
|
+
};
|
|
36
|
+
var __export = (target, all) => {
|
|
37
|
+
for (var name in all)
|
|
38
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
39
|
+
};
|
|
40
|
+
var __copyProps = (to, from, except, desc) => {
|
|
41
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
42
|
+
for (let key of __getOwnPropNames(from))
|
|
43
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
44
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
45
|
+
}
|
|
46
|
+
return to;
|
|
47
|
+
};
|
|
48
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
49
|
+
|
|
50
|
+
// src/index.ts
|
|
51
|
+
var index_exports = {};
|
|
52
|
+
__export(index_exports, {
|
|
53
|
+
Toggle: () => Toggle,
|
|
54
|
+
ToggleGroup: () => ToggleGroup,
|
|
55
|
+
ToggleGroupItem: () => ToggleGroupItem,
|
|
56
|
+
toggleVariants: () => toggleVariants
|
|
57
|
+
});
|
|
58
|
+
module.exports = __toCommonJS(index_exports);
|
|
59
|
+
|
|
60
|
+
// src/toggle.tsx
|
|
61
|
+
var import_react = require("react");
|
|
62
|
+
var import_class_variance_authority = require("class-variance-authority");
|
|
63
|
+
var import_utils = require("@glob-ui/utils");
|
|
64
|
+
var import_hooks = require("@glob-ui/hooks");
|
|
65
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
66
|
+
var toggleVariants = (0, import_class_variance_authority.cva)(
|
|
67
|
+
"inline-flex items-center justify-center gap-2 rounded-glob-md font-medium text-glob-text transition-colors hover:bg-glob-surface-hover focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-glob-ring disabled:pointer-events-none disabled:opacity-50 aria-disabled:pointer-events-none aria-disabled:opacity-50 motion-reduce:transition-none data-[state=on]:bg-glob-primary-light data-[state=on]:text-glob-text",
|
|
68
|
+
{
|
|
69
|
+
variants: {
|
|
70
|
+
variant: {
|
|
71
|
+
default: "bg-transparent",
|
|
72
|
+
outline: "border border-glob-border bg-transparent data-[state=on]:border-glob-primary"
|
|
73
|
+
},
|
|
74
|
+
size: {
|
|
75
|
+
sm: "h-8 px-2 text-xs",
|
|
76
|
+
md: "h-10 px-3 text-sm",
|
|
77
|
+
lg: "h-12 px-4 text-base"
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
defaultVariants: {
|
|
81
|
+
variant: "default",
|
|
82
|
+
size: "md"
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
);
|
|
86
|
+
var Toggle = (0, import_react.forwardRef)(
|
|
87
|
+
(_a, ref) => {
|
|
88
|
+
var _b = _a, { className, variant, size, pressed, defaultPressed = false, onPressedChange, onClick, disabled } = _b, props = __objRest(_b, ["className", "variant", "size", "pressed", "defaultPressed", "onPressedChange", "onClick", "disabled"]);
|
|
89
|
+
const [isPressed, setPressed] = (0, import_hooks.useControllableState)(pressed, defaultPressed, onPressedChange);
|
|
90
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
91
|
+
"button",
|
|
92
|
+
__spreadValues({
|
|
93
|
+
ref,
|
|
94
|
+
type: "button",
|
|
95
|
+
"aria-pressed": isPressed,
|
|
96
|
+
"data-state": isPressed ? "on" : "off",
|
|
97
|
+
disabled,
|
|
98
|
+
onClick: (e) => {
|
|
99
|
+
onClick == null ? void 0 : onClick(e);
|
|
100
|
+
if (!e.defaultPrevented) setPressed(!isPressed);
|
|
101
|
+
},
|
|
102
|
+
className: (0, import_utils.cn)(toggleVariants({ variant, size, className }))
|
|
103
|
+
}, props)
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
);
|
|
107
|
+
Toggle.displayName = "Toggle";
|
|
108
|
+
var ToggleGroupContext = (0, import_react.createContext)(null);
|
|
109
|
+
var ToggleGroup = (0, import_react.forwardRef)((props, ref) => {
|
|
110
|
+
const _a = props, {
|
|
111
|
+
type,
|
|
112
|
+
variant,
|
|
113
|
+
size,
|
|
114
|
+
orientation = "horizontal",
|
|
115
|
+
disabled = false,
|
|
116
|
+
className,
|
|
117
|
+
children,
|
|
118
|
+
value: rawValue,
|
|
119
|
+
defaultValue: rawDefault,
|
|
120
|
+
onValueChange: rawOnValueChange,
|
|
121
|
+
onFocus,
|
|
122
|
+
onKeyDown
|
|
123
|
+
} = _a, rest = __objRest(_a, [
|
|
124
|
+
"type",
|
|
125
|
+
"variant",
|
|
126
|
+
"size",
|
|
127
|
+
"orientation",
|
|
128
|
+
"disabled",
|
|
129
|
+
"className",
|
|
130
|
+
"children",
|
|
131
|
+
"value",
|
|
132
|
+
"defaultValue",
|
|
133
|
+
"onValueChange",
|
|
134
|
+
"onFocus",
|
|
135
|
+
"onKeyDown"
|
|
136
|
+
]);
|
|
137
|
+
const onValueChange = rawOnValueChange;
|
|
138
|
+
const controlled = rawValue === void 0 ? void 0 : type === "single" ? rawValue ? [rawValue] : [] : rawValue;
|
|
139
|
+
const initial = rawDefault === void 0 ? [] : type === "single" ? rawDefault ? [rawDefault] : [] : rawDefault;
|
|
140
|
+
const [pressed, setPressed] = (0, import_hooks.useControllableState)(
|
|
141
|
+
controlled,
|
|
142
|
+
initial,
|
|
143
|
+
(next) => {
|
|
144
|
+
var _a2;
|
|
145
|
+
return onValueChange == null ? void 0 : onValueChange(type === "single" ? (_a2 = next[0]) != null ? _a2 : "" : next);
|
|
146
|
+
}
|
|
147
|
+
);
|
|
148
|
+
function select(item) {
|
|
149
|
+
if (type === "single") setPressed(pressed.includes(item) ? [] : [item]);
|
|
150
|
+
else setPressed(pressed.includes(item) ? pressed.filter((v) => v !== item) : [...pressed, item]);
|
|
151
|
+
}
|
|
152
|
+
const rootRef = (0, import_react.useRef)(null);
|
|
153
|
+
const stopRef = (0, import_react.useRef)(null);
|
|
154
|
+
const roving = (0, import_hooks.useRovingFocus)({ orientation, loop: true });
|
|
155
|
+
function syncTabStop() {
|
|
156
|
+
var _a2, _b;
|
|
157
|
+
const root = rootRef.current;
|
|
158
|
+
if (!root) return;
|
|
159
|
+
const items = Array.from(root.querySelectorAll("[data-roving-item]"));
|
|
160
|
+
const enabled = items.filter((el) => !el.disabled);
|
|
161
|
+
const stop = (_b = (_a2 = stopRef.current && enabled.includes(stopRef.current) ? stopRef.current : void 0) != null ? _a2 : enabled.find((el) => el.getAttribute("aria-pressed") === "true")) != null ? _b : enabled[0];
|
|
162
|
+
for (const el of items) el.tabIndex = el === stop ? 0 : -1;
|
|
163
|
+
stopRef.current = stop != null ? stop : null;
|
|
164
|
+
}
|
|
165
|
+
(0, import_react.useLayoutEffect)(syncTabStop);
|
|
166
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ToggleGroupContext.Provider, { value: { pressed, select, variant, size, disabled }, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
167
|
+
"div",
|
|
168
|
+
__spreadProps(__spreadValues({
|
|
169
|
+
ref: (node) => {
|
|
170
|
+
rootRef.current = node;
|
|
171
|
+
if (typeof ref === "function") ref(node);
|
|
172
|
+
else if (ref) ref.current = node;
|
|
173
|
+
},
|
|
174
|
+
role: "group",
|
|
175
|
+
"aria-disabled": disabled || void 0
|
|
176
|
+
}, rest), {
|
|
177
|
+
"data-roving-root": "",
|
|
178
|
+
onKeyDown: (e) => {
|
|
179
|
+
onKeyDown == null ? void 0 : onKeyDown(e);
|
|
180
|
+
roving.onKeyDown(e);
|
|
181
|
+
},
|
|
182
|
+
onFocus: (e) => {
|
|
183
|
+
onFocus == null ? void 0 : onFocus(e);
|
|
184
|
+
stopRef.current = e.target;
|
|
185
|
+
syncTabStop();
|
|
186
|
+
},
|
|
187
|
+
className: (0, import_utils.cn)("inline-flex items-center gap-1", orientation === "vertical" && "flex-col", className),
|
|
188
|
+
children
|
|
189
|
+
})
|
|
190
|
+
) });
|
|
191
|
+
});
|
|
192
|
+
ToggleGroup.displayName = "ToggleGroup";
|
|
193
|
+
var ToggleGroupItem = (0, import_react.forwardRef)(
|
|
194
|
+
(_a, ref) => {
|
|
195
|
+
var _b = _a, { className, value, onClick, disabled, children } = _b, props = __objRest(_b, ["className", "value", "onClick", "disabled", "children"]);
|
|
196
|
+
const ctx = (0, import_react.useContext)(ToggleGroupContext);
|
|
197
|
+
if (!ctx) throw new Error("ToggleGroupItem must be used inside ToggleGroup");
|
|
198
|
+
const on = ctx.pressed.includes(value);
|
|
199
|
+
const isDisabled = disabled || ctx.disabled;
|
|
200
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
201
|
+
"button",
|
|
202
|
+
__spreadProps(__spreadValues({
|
|
203
|
+
ref,
|
|
204
|
+
type: "button",
|
|
205
|
+
"data-roving-item": "",
|
|
206
|
+
"aria-pressed": on,
|
|
207
|
+
"data-state": on ? "on" : "off",
|
|
208
|
+
disabled: isDisabled,
|
|
209
|
+
onClick: (e) => {
|
|
210
|
+
onClick == null ? void 0 : onClick(e);
|
|
211
|
+
if (!e.defaultPrevented) ctx.select(value);
|
|
212
|
+
},
|
|
213
|
+
className: (0, import_utils.cn)(toggleVariants({ variant: ctx.variant, size: ctx.size, className }))
|
|
214
|
+
}, props), {
|
|
215
|
+
children
|
|
216
|
+
})
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
);
|
|
220
|
+
ToggleGroupItem.displayName = "ToggleGroupItem";
|
|
221
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
222
|
+
0 && (module.exports = {
|
|
223
|
+
Toggle,
|
|
224
|
+
ToggleGroup,
|
|
225
|
+
ToggleGroupItem,
|
|
226
|
+
toggleVariants
|
|
227
|
+
});
|
|
228
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/toggle.tsx"],"sourcesContent":["export { Toggle, ToggleGroup, ToggleGroupItem, toggleVariants } from \"./toggle\"\nexport type { ToggleProps, ToggleGroupProps, ToggleGroupSingleProps, ToggleGroupMultipleProps, ToggleGroupItemProps } from \"./toggle\"\n","\"use client\"\n\nimport { createContext, forwardRef, useContext, useLayoutEffect, useRef } from \"react\"\nimport { cva, type VariantProps } from \"class-variance-authority\"\nimport { cn } from \"@glob-ui/utils\"\nimport { useControllableState, useRovingFocus } from \"@glob-ui/hooks\"\n\nconst toggleVariants = cva(\n \"inline-flex items-center justify-center gap-2 rounded-glob-md font-medium text-glob-text transition-colors hover:bg-glob-surface-hover focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-glob-ring disabled:pointer-events-none disabled:opacity-50 aria-disabled:pointer-events-none aria-disabled:opacity-50 motion-reduce:transition-none data-[state=on]:bg-glob-primary-light data-[state=on]:text-glob-text\",\n {\n variants: {\n variant: {\n default: \"bg-transparent\",\n outline: \"border border-glob-border bg-transparent data-[state=on]:border-glob-primary\",\n },\n size: {\n sm: \"h-8 px-2 text-xs\",\n md: \"h-10 px-3 text-sm\",\n lg: \"h-12 px-4 text-base\",\n },\n },\n defaultVariants: {\n variant: \"default\",\n size: \"md\",\n },\n }\n)\n\nexport interface ToggleProps\n extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, \"value\">,\n VariantProps<typeof toggleVariants> {\n pressed?: boolean\n defaultPressed?: boolean\n onPressedChange?: (pressed: boolean) => void\n}\n\n/** A button with an on and off state (`aria-pressed`). Give it an accessible name when it has only an icon. */\nconst Toggle = forwardRef<HTMLButtonElement, ToggleProps>(\n ({ className, variant, size, pressed, defaultPressed = false, onPressedChange, onClick, disabled, ...props }, ref) => {\n const [isPressed, setPressed] = useControllableState(pressed, defaultPressed, onPressedChange)\n return (\n <button\n ref={ref}\n type=\"button\"\n aria-pressed={isPressed}\n data-state={isPressed ? \"on\" : \"off\"}\n disabled={disabled}\n onClick={(e) => {\n onClick?.(e)\n if (!e.defaultPrevented) setPressed(!isPressed)\n }}\n className={cn(toggleVariants({ variant, size, className }))}\n {...props}\n />\n )\n }\n)\nToggle.displayName = \"Toggle\"\n\ninterface ToggleGroupContextValue {\n pressed: string[]\n select: (value: string) => void\n variant: VariantProps<typeof toggleVariants>[\"variant\"]\n size: VariantProps<typeof toggleVariants>[\"size\"]\n disabled: boolean\n}\n\nconst ToggleGroupContext = createContext<ToggleGroupContextValue | null>(null)\n\ntype CommonGroupProps = Omit<React.HTMLAttributes<HTMLDivElement>, \"defaultValue\" | \"onChange\"> &\n VariantProps<typeof toggleVariants> & {\n orientation?: \"horizontal\" | \"vertical\"\n disabled?: boolean\n }\n\nexport type ToggleGroupSingleProps = CommonGroupProps & {\n type: \"single\"\n /** The pressed item's value, or \"\" when none is pressed. */\n value?: string\n defaultValue?: string\n onValueChange?: (value: string) => void\n}\n\nexport type ToggleGroupMultipleProps = CommonGroupProps & {\n type: \"multiple\"\n value?: string[]\n defaultValue?: string[]\n onValueChange?: (value: string[]) => void\n}\n\nexport type ToggleGroupProps = ToggleGroupSingleProps | ToggleGroupMultipleProps\n\n/**\n * A row of toggles. `single` lets one item be pressed (pressing it again clears the choice);\n * `multiple` lets several be pressed. Items are `aria-pressed` buttons in one tab stop: arrows move focus.\n */\nconst ToggleGroup = forwardRef<HTMLDivElement, ToggleGroupProps>((props, ref) => {\n const {\n type,\n variant,\n size,\n orientation = \"horizontal\",\n disabled = false,\n className,\n children,\n value: rawValue,\n defaultValue: rawDefault,\n onValueChange: rawOnValueChange,\n onFocus,\n onKeyDown,\n ...rest\n } = props\n const onValueChange = rawOnValueChange as ((v: string | string[]) => void) | undefined\n\n // Internally a list of values; single mode maps \"\" to [] and back.\n const controlled = rawValue === undefined ? undefined : type === \"single\" ? (rawValue ? [rawValue as string] : []) : (rawValue as string[])\n const initial = rawDefault === undefined ? [] : type === \"single\" ? (rawDefault ? [rawDefault as string] : []) : (rawDefault as string[])\n const [pressed, setPressed] = useControllableState<string[]>(controlled, initial, (next) =>\n onValueChange?.(type === \"single\" ? (next[0] ?? \"\") : next)\n )\n\n function select(item: string) {\n if (type === \"single\") setPressed(pressed.includes(item) ? [] : [item])\n else setPressed(pressed.includes(item) ? pressed.filter((v) => v !== item) : [...pressed, item])\n }\n\n const rootRef = useRef<HTMLDivElement | null>(null)\n const stopRef = useRef<HTMLElement | null>(null)\n const roving = useRovingFocus({ orientation, loop: true })\n\n // One tab stop: the last focused item, else the first pressed one, else the first enabled one.\n function syncTabStop() {\n const root = rootRef.current\n if (!root) return\n const items = Array.from(root.querySelectorAll<HTMLElement>(\"[data-roving-item]\"))\n const enabled = items.filter((el) => !(el as HTMLButtonElement).disabled)\n const stop =\n (stopRef.current && enabled.includes(stopRef.current) ? stopRef.current : undefined) ??\n enabled.find((el) => el.getAttribute(\"aria-pressed\") === \"true\") ??\n enabled[0]\n for (const el of items) el.tabIndex = el === stop ? 0 : -1\n stopRef.current = stop ?? null\n }\n useLayoutEffect(syncTabStop)\n\n return (\n <ToggleGroupContext.Provider value={{ pressed, select, variant, size, disabled }}>\n <div\n ref={(node) => {\n rootRef.current = node\n if (typeof ref === \"function\") ref(node)\n else if (ref) ref.current = node\n }}\n role=\"group\"\n aria-disabled={disabled || undefined}\n {...rest}\n data-roving-root=\"\"\n onKeyDown={(e) => {\n onKeyDown?.(e)\n roving.onKeyDown(e)\n }}\n onFocus={(e) => {\n onFocus?.(e)\n stopRef.current = e.target as HTMLElement\n syncTabStop()\n }}\n className={cn(\"inline-flex items-center gap-1\", orientation === \"vertical\" && \"flex-col\", className)}\n >\n {children}\n </div>\n </ToggleGroupContext.Provider>\n )\n})\nToggleGroup.displayName = \"ToggleGroup\"\n\nexport interface ToggleGroupItemProps extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, \"value\"> {\n /** The value this item stands for in the group's `value`. */\n value: string\n}\n\nconst ToggleGroupItem = forwardRef<HTMLButtonElement, ToggleGroupItemProps>(\n ({ className, value, onClick, disabled, children, ...props }, ref) => {\n const ctx = useContext(ToggleGroupContext)\n if (!ctx) throw new Error(\"ToggleGroupItem must be used inside ToggleGroup\")\n const on = ctx.pressed.includes(value)\n const isDisabled = disabled || ctx.disabled\n return (\n <button\n ref={ref}\n type=\"button\"\n data-roving-item=\"\"\n aria-pressed={on}\n data-state={on ? \"on\" : \"off\"}\n disabled={isDisabled}\n onClick={(e) => {\n onClick?.(e)\n if (!e.defaultPrevented) ctx.select(value)\n }}\n className={cn(toggleVariants({ variant: ctx.variant, size: ctx.size, className }))}\n {...props}\n >\n {children}\n </button>\n )\n }\n)\nToggleGroupItem.displayName = \"ToggleGroupItem\"\n\nexport { Toggle, ToggleGroup, ToggleGroupItem, toggleVariants }\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEA,mBAA+E;AAC/E,sCAAuC;AACvC,mBAAmB;AACnB,mBAAqD;AAoC/C;AAlCN,IAAM,qBAAiB;AAAA,EACrB;AAAA,EACA;AAAA,IACE,UAAU;AAAA,MACR,SAAS;AAAA,QACP,SAAS;AAAA,QACT,SAAS;AAAA,MACX;AAAA,MACA,MAAM;AAAA,QACJ,IAAI;AAAA,QACJ,IAAI;AAAA,QACJ,IAAI;AAAA,MACN;AAAA,IACF;AAAA,IACA,iBAAiB;AAAA,MACf,SAAS;AAAA,MACT,MAAM;AAAA,IACR;AAAA,EACF;AACF;AAWA,IAAM,aAAS;AAAA,EACb,CAAC,IAA6G,QAAQ;AAArH,iBAAE,aAAW,SAAS,MAAM,SAAS,iBAAiB,OAAO,iBAAiB,SAAS,SAtC1F,IAsCG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e,YAAY;AAAA,OACvB,OARL;AAAA,MASC,oBAAiB;AAAA,MACjB,WAAW,CAAC,MAAM;AAChB,+CAAY;AACZ,eAAO,UAAU,CAAC;AAAA,MACpB;AAAA,MACA,SAAS,CAAC,MAAM;AACd,2CAAU;AACV,gBAAQ,UAAU,EAAE;AACpB,oBAAY;AAAA,MACd;AAAA,MACA,eAAW,iBAAG,kCAAkC,gBAAgB,cAAc,YAAY,SAAS;AAAA,MAElG;AAAA;AAAA,EACH,GACF;AAEJ,CAAC;AACD,YAAY,cAAc;AAO1B,IAAM,sBAAkB;AAAA,EACtB,CAAC,IAA6D,QAAQ;AAArE,iBAAE,aAAW,OAAO,SAAS,UAAU,SArL1C,IAqLG,IAAoD,kBAApD,IAAoD,CAAlD,aAAW,SAAO,WAAS,YAAU;AACtC,UAAM,UAAM,yBAAW,kBAAkB;AACzC,QAAI,CAAC,IAAK,OAAM,IAAI,MAAM,iDAAiD;AAC3E,UAAM,KAAK,IAAI,QAAQ,SAAS,KAAK;AACrC,UAAM,aAAa,YAAY,IAAI;AACnC,WACE;AAAA,MAAC;AAAA;AAAA,QACC;AAAA,QACA,MAAK;AAAA,QACL,oBAAiB;AAAA,QACjB,gBAAc;AAAA,QACd,cAAY,KAAK,OAAO;AAAA,QACxB,UAAU;AAAA,QACV,SAAS,CAAC,MAAM;AACd,6CAAU;AACV,cAAI,CAAC,EAAE,iBAAkB,KAAI,OAAO,KAAK;AAAA,QAC3C;AAAA,QACA,eAAW,iBAAG,eAAe,EAAE,SAAS,IAAI,SAAS,MAAM,IAAI,MAAM,UAAU,CAAC,CAAC;AAAA,SAC7E,QAZL;AAAA,QAcE;AAAA;AAAA,IACH;AAAA,EAEJ;AACF;AACA,gBAAgB,cAAc;","names":["_a"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defProps = Object.defineProperties;
|
|
4
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
5
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
8
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9
|
+
var __spreadValues = (a, b) => {
|
|
10
|
+
for (var prop in b || (b = {}))
|
|
11
|
+
if (__hasOwnProp.call(b, prop))
|
|
12
|
+
__defNormalProp(a, prop, b[prop]);
|
|
13
|
+
if (__getOwnPropSymbols)
|
|
14
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
15
|
+
if (__propIsEnum.call(b, prop))
|
|
16
|
+
__defNormalProp(a, prop, b[prop]);
|
|
17
|
+
}
|
|
18
|
+
return a;
|
|
19
|
+
};
|
|
20
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
21
|
+
var __objRest = (source, exclude) => {
|
|
22
|
+
var target = {};
|
|
23
|
+
for (var prop in source)
|
|
24
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
25
|
+
target[prop] = source[prop];
|
|
26
|
+
if (source != null && __getOwnPropSymbols)
|
|
27
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
28
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
29
|
+
target[prop] = source[prop];
|
|
30
|
+
}
|
|
31
|
+
return target;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// src/toggle.tsx
|
|
35
|
+
import { createContext, forwardRef, useContext, useLayoutEffect, useRef } from "react";
|
|
36
|
+
import { cva } from "class-variance-authority";
|
|
37
|
+
import { cn } from "@glob-ui/utils";
|
|
38
|
+
import { useControllableState, useRovingFocus } from "@glob-ui/hooks";
|
|
39
|
+
import { jsx } from "react/jsx-runtime";
|
|
40
|
+
var toggleVariants = cva(
|
|
41
|
+
"inline-flex items-center justify-center gap-2 rounded-glob-md font-medium text-glob-text transition-colors hover:bg-glob-surface-hover focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-glob-ring disabled:pointer-events-none disabled:opacity-50 aria-disabled:pointer-events-none aria-disabled:opacity-50 motion-reduce:transition-none data-[state=on]:bg-glob-primary-light data-[state=on]:text-glob-text",
|
|
42
|
+
{
|
|
43
|
+
variants: {
|
|
44
|
+
variant: {
|
|
45
|
+
default: "bg-transparent",
|
|
46
|
+
outline: "border border-glob-border bg-transparent data-[state=on]:border-glob-primary"
|
|
47
|
+
},
|
|
48
|
+
size: {
|
|
49
|
+
sm: "h-8 px-2 text-xs",
|
|
50
|
+
md: "h-10 px-3 text-sm",
|
|
51
|
+
lg: "h-12 px-4 text-base"
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
defaultVariants: {
|
|
55
|
+
variant: "default",
|
|
56
|
+
size: "md"
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
);
|
|
60
|
+
var Toggle = forwardRef(
|
|
61
|
+
(_a, ref) => {
|
|
62
|
+
var _b = _a, { className, variant, size, pressed, defaultPressed = false, onPressedChange, onClick, disabled } = _b, props = __objRest(_b, ["className", "variant", "size", "pressed", "defaultPressed", "onPressedChange", "onClick", "disabled"]);
|
|
63
|
+
const [isPressed, setPressed] = useControllableState(pressed, defaultPressed, onPressedChange);
|
|
64
|
+
return /* @__PURE__ */ jsx(
|
|
65
|
+
"button",
|
|
66
|
+
__spreadValues({
|
|
67
|
+
ref,
|
|
68
|
+
type: "button",
|
|
69
|
+
"aria-pressed": isPressed,
|
|
70
|
+
"data-state": isPressed ? "on" : "off",
|
|
71
|
+
disabled,
|
|
72
|
+
onClick: (e) => {
|
|
73
|
+
onClick == null ? void 0 : onClick(e);
|
|
74
|
+
if (!e.defaultPrevented) setPressed(!isPressed);
|
|
75
|
+
},
|
|
76
|
+
className: cn(toggleVariants({ variant, size, className }))
|
|
77
|
+
}, props)
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
);
|
|
81
|
+
Toggle.displayName = "Toggle";
|
|
82
|
+
var ToggleGroupContext = createContext(null);
|
|
83
|
+
var ToggleGroup = forwardRef((props, ref) => {
|
|
84
|
+
const _a = props, {
|
|
85
|
+
type,
|
|
86
|
+
variant,
|
|
87
|
+
size,
|
|
88
|
+
orientation = "horizontal",
|
|
89
|
+
disabled = false,
|
|
90
|
+
className,
|
|
91
|
+
children,
|
|
92
|
+
value: rawValue,
|
|
93
|
+
defaultValue: rawDefault,
|
|
94
|
+
onValueChange: rawOnValueChange,
|
|
95
|
+
onFocus,
|
|
96
|
+
onKeyDown
|
|
97
|
+
} = _a, rest = __objRest(_a, [
|
|
98
|
+
"type",
|
|
99
|
+
"variant",
|
|
100
|
+
"size",
|
|
101
|
+
"orientation",
|
|
102
|
+
"disabled",
|
|
103
|
+
"className",
|
|
104
|
+
"children",
|
|
105
|
+
"value",
|
|
106
|
+
"defaultValue",
|
|
107
|
+
"onValueChange",
|
|
108
|
+
"onFocus",
|
|
109
|
+
"onKeyDown"
|
|
110
|
+
]);
|
|
111
|
+
const onValueChange = rawOnValueChange;
|
|
112
|
+
const controlled = rawValue === void 0 ? void 0 : type === "single" ? rawValue ? [rawValue] : [] : rawValue;
|
|
113
|
+
const initial = rawDefault === void 0 ? [] : type === "single" ? rawDefault ? [rawDefault] : [] : rawDefault;
|
|
114
|
+
const [pressed, setPressed] = useControllableState(
|
|
115
|
+
controlled,
|
|
116
|
+
initial,
|
|
117
|
+
(next) => {
|
|
118
|
+
var _a2;
|
|
119
|
+
return onValueChange == null ? void 0 : onValueChange(type === "single" ? (_a2 = next[0]) != null ? _a2 : "" : next);
|
|
120
|
+
}
|
|
121
|
+
);
|
|
122
|
+
function select(item) {
|
|
123
|
+
if (type === "single") setPressed(pressed.includes(item) ? [] : [item]);
|
|
124
|
+
else setPressed(pressed.includes(item) ? pressed.filter((v) => v !== item) : [...pressed, item]);
|
|
125
|
+
}
|
|
126
|
+
const rootRef = useRef(null);
|
|
127
|
+
const stopRef = useRef(null);
|
|
128
|
+
const roving = useRovingFocus({ orientation, loop: true });
|
|
129
|
+
function syncTabStop() {
|
|
130
|
+
var _a2, _b;
|
|
131
|
+
const root = rootRef.current;
|
|
132
|
+
if (!root) return;
|
|
133
|
+
const items = Array.from(root.querySelectorAll("[data-roving-item]"));
|
|
134
|
+
const enabled = items.filter((el) => !el.disabled);
|
|
135
|
+
const stop = (_b = (_a2 = stopRef.current && enabled.includes(stopRef.current) ? stopRef.current : void 0) != null ? _a2 : enabled.find((el) => el.getAttribute("aria-pressed") === "true")) != null ? _b : enabled[0];
|
|
136
|
+
for (const el of items) el.tabIndex = el === stop ? 0 : -1;
|
|
137
|
+
stopRef.current = stop != null ? stop : null;
|
|
138
|
+
}
|
|
139
|
+
useLayoutEffect(syncTabStop);
|
|
140
|
+
return /* @__PURE__ */ jsx(ToggleGroupContext.Provider, { value: { pressed, select, variant, size, disabled }, children: /* @__PURE__ */ jsx(
|
|
141
|
+
"div",
|
|
142
|
+
__spreadProps(__spreadValues({
|
|
143
|
+
ref: (node) => {
|
|
144
|
+
rootRef.current = node;
|
|
145
|
+
if (typeof ref === "function") ref(node);
|
|
146
|
+
else if (ref) ref.current = node;
|
|
147
|
+
},
|
|
148
|
+
role: "group",
|
|
149
|
+
"aria-disabled": disabled || void 0
|
|
150
|
+
}, rest), {
|
|
151
|
+
"data-roving-root": "",
|
|
152
|
+
onKeyDown: (e) => {
|
|
153
|
+
onKeyDown == null ? void 0 : onKeyDown(e);
|
|
154
|
+
roving.onKeyDown(e);
|
|
155
|
+
},
|
|
156
|
+
onFocus: (e) => {
|
|
157
|
+
onFocus == null ? void 0 : onFocus(e);
|
|
158
|
+
stopRef.current = e.target;
|
|
159
|
+
syncTabStop();
|
|
160
|
+
},
|
|
161
|
+
className: cn("inline-flex items-center gap-1", orientation === "vertical" && "flex-col", className),
|
|
162
|
+
children
|
|
163
|
+
})
|
|
164
|
+
) });
|
|
165
|
+
});
|
|
166
|
+
ToggleGroup.displayName = "ToggleGroup";
|
|
167
|
+
var ToggleGroupItem = forwardRef(
|
|
168
|
+
(_a, ref) => {
|
|
169
|
+
var _b = _a, { className, value, onClick, disabled, children } = _b, props = __objRest(_b, ["className", "value", "onClick", "disabled", "children"]);
|
|
170
|
+
const ctx = useContext(ToggleGroupContext);
|
|
171
|
+
if (!ctx) throw new Error("ToggleGroupItem must be used inside ToggleGroup");
|
|
172
|
+
const on = ctx.pressed.includes(value);
|
|
173
|
+
const isDisabled = disabled || ctx.disabled;
|
|
174
|
+
return /* @__PURE__ */ jsx(
|
|
175
|
+
"button",
|
|
176
|
+
__spreadProps(__spreadValues({
|
|
177
|
+
ref,
|
|
178
|
+
type: "button",
|
|
179
|
+
"data-roving-item": "",
|
|
180
|
+
"aria-pressed": on,
|
|
181
|
+
"data-state": on ? "on" : "off",
|
|
182
|
+
disabled: isDisabled,
|
|
183
|
+
onClick: (e) => {
|
|
184
|
+
onClick == null ? void 0 : onClick(e);
|
|
185
|
+
if (!e.defaultPrevented) ctx.select(value);
|
|
186
|
+
},
|
|
187
|
+
className: cn(toggleVariants({ variant: ctx.variant, size: ctx.size, className }))
|
|
188
|
+
}, props), {
|
|
189
|
+
children
|
|
190
|
+
})
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
);
|
|
194
|
+
ToggleGroupItem.displayName = "ToggleGroupItem";
|
|
195
|
+
export {
|
|
196
|
+
Toggle,
|
|
197
|
+
ToggleGroup,
|
|
198
|
+
ToggleGroupItem,
|
|
199
|
+
toggleVariants
|
|
200
|
+
};
|
|
201
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/toggle.tsx"],"sourcesContent":["\"use client\"\n\nimport { createContext, forwardRef, useContext, useLayoutEffect, useRef } from \"react\"\nimport { cva, type VariantProps } from \"class-variance-authority\"\nimport { cn } from \"@glob-ui/utils\"\nimport { useControllableState, useRovingFocus } from \"@glob-ui/hooks\"\n\nconst toggleVariants = cva(\n \"inline-flex items-center justify-center gap-2 rounded-glob-md font-medium text-glob-text transition-colors hover:bg-glob-surface-hover focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-glob-ring disabled:pointer-events-none disabled:opacity-50 aria-disabled:pointer-events-none aria-disabled:opacity-50 motion-reduce:transition-none data-[state=on]:bg-glob-primary-light data-[state=on]:text-glob-text\",\n {\n variants: {\n variant: {\n default: \"bg-transparent\",\n outline: \"border border-glob-border bg-transparent data-[state=on]:border-glob-primary\",\n },\n size: {\n sm: \"h-8 px-2 text-xs\",\n md: \"h-10 px-3 text-sm\",\n lg: \"h-12 px-4 text-base\",\n },\n },\n defaultVariants: {\n variant: \"default\",\n size: \"md\",\n },\n }\n)\n\nexport interface ToggleProps\n extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, \"value\">,\n VariantProps<typeof toggleVariants> {\n pressed?: boolean\n defaultPressed?: boolean\n onPressedChange?: (pressed: boolean) => void\n}\n\n/** A button with an on and off state (`aria-pressed`). Give it an accessible name when it has only an icon. */\nconst Toggle = forwardRef<HTMLButtonElement, ToggleProps>(\n ({ className, variant, size, pressed, defaultPressed = false, onPressedChange, onClick, disabled, ...props }, ref) => {\n const [isPressed, setPressed] = useControllableState(pressed, defaultPressed, onPressedChange)\n return (\n <button\n ref={ref}\n type=\"button\"\n aria-pressed={isPressed}\n data-state={isPressed ? \"on\" : \"off\"}\n disabled={disabled}\n onClick={(e) => {\n onClick?.(e)\n if (!e.defaultPrevented) setPressed(!isPressed)\n }}\n className={cn(toggleVariants({ variant, size, className }))}\n {...props}\n />\n )\n }\n)\nToggle.displayName = \"Toggle\"\n\ninterface ToggleGroupContextValue {\n pressed: string[]\n select: (value: string) => void\n variant: VariantProps<typeof toggleVariants>[\"variant\"]\n size: VariantProps<typeof toggleVariants>[\"size\"]\n disabled: boolean\n}\n\nconst ToggleGroupContext = createContext<ToggleGroupContextValue | null>(null)\n\ntype CommonGroupProps = Omit<React.HTMLAttributes<HTMLDivElement>, \"defaultValue\" | \"onChange\"> &\n VariantProps<typeof toggleVariants> & {\n orientation?: \"horizontal\" | \"vertical\"\n disabled?: boolean\n }\n\nexport type ToggleGroupSingleProps = CommonGroupProps & {\n type: \"single\"\n /** The pressed item's value, or \"\" when none is pressed. */\n value?: string\n defaultValue?: string\n onValueChange?: (value: string) => void\n}\n\nexport type ToggleGroupMultipleProps = CommonGroupProps & {\n type: \"multiple\"\n value?: string[]\n defaultValue?: string[]\n onValueChange?: (value: string[]) => void\n}\n\nexport type ToggleGroupProps = ToggleGroupSingleProps | ToggleGroupMultipleProps\n\n/**\n * A row of toggles. `single` lets one item be pressed (pressing it again clears the choice);\n * `multiple` lets several be pressed. Items are `aria-pressed` buttons in one tab stop: arrows move focus.\n */\nconst ToggleGroup = forwardRef<HTMLDivElement, ToggleGroupProps>((props, ref) => {\n const {\n type,\n variant,\n size,\n orientation = \"horizontal\",\n disabled = false,\n className,\n children,\n value: rawValue,\n defaultValue: rawDefault,\n onValueChange: rawOnValueChange,\n onFocus,\n onKeyDown,\n ...rest\n } = props\n const onValueChange = rawOnValueChange as ((v: string | string[]) => void) | undefined\n\n // Internally a list of values; single mode maps \"\" to [] and back.\n const controlled = rawValue === undefined ? undefined : type === \"single\" ? (rawValue ? [rawValue as string] : []) : (rawValue as string[])\n const initial = rawDefault === undefined ? [] : type === \"single\" ? (rawDefault ? [rawDefault as string] : []) : (rawDefault as string[])\n const [pressed, setPressed] = useControllableState<string[]>(controlled, initial, (next) =>\n onValueChange?.(type === \"single\" ? (next[0] ?? \"\") : next)\n )\n\n function select(item: string) {\n if (type === \"single\") setPressed(pressed.includes(item) ? [] : [item])\n else setPressed(pressed.includes(item) ? pressed.filter((v) => v !== item) : [...pressed, item])\n }\n\n const rootRef = useRef<HTMLDivElement | null>(null)\n const stopRef = useRef<HTMLElement | null>(null)\n const roving = useRovingFocus({ orientation, loop: true })\n\n // One tab stop: the last focused item, else the first pressed one, else the first enabled one.\n function syncTabStop() {\n const root = rootRef.current\n if (!root) return\n const items = Array.from(root.querySelectorAll<HTMLElement>(\"[data-roving-item]\"))\n const enabled = items.filter((el) => !(el as HTMLButtonElement).disabled)\n const stop =\n (stopRef.current && enabled.includes(stopRef.current) ? stopRef.current : undefined) ??\n enabled.find((el) => el.getAttribute(\"aria-pressed\") === \"true\") ??\n enabled[0]\n for (const el of items) el.tabIndex = el === stop ? 0 : -1\n stopRef.current = stop ?? null\n }\n useLayoutEffect(syncTabStop)\n\n return (\n <ToggleGroupContext.Provider value={{ pressed, select, variant, size, disabled }}>\n <div\n ref={(node) => {\n rootRef.current = node\n if (typeof ref === \"function\") ref(node)\n else if (ref) ref.current = node\n }}\n role=\"group\"\n aria-disabled={disabled || undefined}\n {...rest}\n data-roving-root=\"\"\n onKeyDown={(e) => {\n onKeyDown?.(e)\n roving.onKeyDown(e)\n }}\n onFocus={(e) => {\n onFocus?.(e)\n stopRef.current = e.target as HTMLElement\n syncTabStop()\n }}\n className={cn(\"inline-flex items-center gap-1\", orientation === \"vertical\" && \"flex-col\", className)}\n >\n {children}\n </div>\n </ToggleGroupContext.Provider>\n )\n})\nToggleGroup.displayName = \"ToggleGroup\"\n\nexport interface ToggleGroupItemProps extends Omit<React.ButtonHTMLAttributes<HTMLButtonElement>, \"value\"> {\n /** The value this item stands for in the group's `value`. */\n value: string\n}\n\nconst ToggleGroupItem = forwardRef<HTMLButtonElement, ToggleGroupItemProps>(\n ({ className, value, onClick, disabled, children, ...props }, ref) => {\n const ctx = useContext(ToggleGroupContext)\n if (!ctx) throw new Error(\"ToggleGroupItem must be used inside ToggleGroup\")\n const on = ctx.pressed.includes(value)\n const isDisabled = disabled || ctx.disabled\n return (\n <button\n ref={ref}\n type=\"button\"\n data-roving-item=\"\"\n aria-pressed={on}\n data-state={on ? \"on\" : \"off\"}\n disabled={isDisabled}\n onClick={(e) => {\n onClick?.(e)\n if (!e.defaultPrevented) ctx.select(value)\n }}\n className={cn(toggleVariants({ variant: ctx.variant, size: ctx.size, className }))}\n {...props}\n >\n {children}\n </button>\n )\n }\n)\nToggleGroupItem.displayName = \"ToggleGroupItem\"\n\nexport { Toggle, ToggleGroup, ToggleGroupItem, toggleVariants }\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,SAAS,eAAe,YAAY,YAAY,iBAAiB,cAAc;AAC/E,SAAS,WAA8B;AACvC,SAAS,UAAU;AACnB,SAAS,sBAAsB,sBAAsB;AAoC/C;AAlCN,IAAM,iBAAiB;AAAA,EACrB;AAAA,EACA;AAAA,IACE,UAAU;AAAA,MACR,SAAS;AAAA,QACP,SAAS;AAAA,QACT,SAAS;AAAA,MACX;AAAA,MACA,MAAM;AAAA,QACJ,IAAI;AAAA,QACJ,IAAI;AAAA,QACJ,IAAI;AAAA,MACN;AAAA,IACF;AAAA,IACA,iBAAiB;AAAA,MACf,SAAS;AAAA,MACT,MAAM;AAAA,IACR;AAAA,EACF;AACF;AAWA,IAAM,SAAS;AAAA,EACb,CAAC,IAA6G,QAAQ;AAArH,iBAAE,aAAW,SAAS,MAAM,SAAS,iBAAiB,OAAO,iBAAiB,SAAS,SAtC1F,IAsCG,IAAoG,kBAApG,IAAoG,CAAlG,aAAW,WAAS,QAAM,WAAS,kBAAwB,mBAAiB,WAAS;AACtF,UAAM,CAAC,WAAW,UAAU,IAAI,qBAAqB,SAAS,gBAAgB,eAAe;AAC7F,WACE;AAAA,MAAC;AAAA;AAAA,QACC;AAAA,QACA,MAAK;AAAA,QACL,gBAAc;AAAA,QACd,cAAY,YAAY,OAAO;AAAA,QAC/B;AAAA,QACA,SAAS,CAAC,MAAM;AACd,6CAAU;AACV,cAAI,CAAC,EAAE,iBAAkB,YAAW,CAAC,SAAS;AAAA,QAChD;AAAA,QACA,WAAW,GAAG,eAAe,EAAE,SAAS,MAAM,UAAU,CAAC,CAAC;AAAA,SACtD;AAAA,IACN;AAAA,EAEJ;AACF;AACA,OA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AAS,YAAU;AACtC,UAAM,MAAM,WAAW,kBAAkB;AACzC,QAAI,CAAC,IAAK,OAAM,IAAI,MAAM,iDAAiD;AAC3E,UAAM,KAAK,IAAI,QAAQ,SAAS,KAAK;AACrC,UAAM,aAAa,YAAY,IAAI;AACnC,WACE;AAAA,MAAC;AAAA;AAAA,QACC;AAAA,QACA,MAAK;AAAA,QACL,oBAAiB;AAAA,QACjB,gBAAc;AAAA,QACd,cAAY,KAAK,OAAO;AAAA,QACxB,UAAU;AAAA,QACV,SAAS,CAAC,MAAM;AACd,6CAAU;AACV,cAAI,CAAC,EAAE,iBAAkB,KAAI,OAAO,KAAK;AAAA,QAC3C;AAAA,QACA,WAAW,GAAG,eAAe,EAAE,SAAS,IAAI,SAAS,MAAM,IAAI,MAAM,UAAU,CAAC,CAAC;AAAA,SAC7E,QAZL;AAAA,QAcE;AAAA;AAAA,IACH;AAAA,EAEJ;AACF;AACA,gBAAgB,cAAc;","names":["_a"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,69 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@glob-ui/toggle",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"main": "./dist/index.js",
|
|
5
|
+
"module": "./dist/index.mjs",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": {
|
|
10
|
+
"types": "./dist/index.d.mts",
|
|
11
|
+
"default": "./dist/index.mjs"
|
|
12
|
+
},
|
|
13
|
+
"require": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"./package.json": "./package.json"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist/",
|
|
22
|
+
"LICENSE",
|
|
23
|
+
"README.md"
|
|
24
|
+
],
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"react": "^19"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@glob-ui/hooks": "0.2.0",
|
|
30
|
+
"@glob-ui/utils": "0.2.0",
|
|
31
|
+
"class-variance-authority": "^0.7.1"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"tsup": "^8.5.1"
|
|
35
|
+
},
|
|
36
|
+
"description": "A button with a pressed state, and a group of them with single or multiple selection.",
|
|
37
|
+
"keywords": [
|
|
38
|
+
"react",
|
|
39
|
+
"glob-ui",
|
|
40
|
+
"components",
|
|
41
|
+
"accessible",
|
|
42
|
+
"tailwind",
|
|
43
|
+
"toggle"
|
|
44
|
+
],
|
|
45
|
+
"license": "MIT",
|
|
46
|
+
"author": "Kevin Martínez E",
|
|
47
|
+
"repository": {
|
|
48
|
+
"type": "git",
|
|
49
|
+
"url": "git+https://github.com/kevin-martineze/GlobUI.git",
|
|
50
|
+
"directory": "packages/toggle"
|
|
51
|
+
},
|
|
52
|
+
"homepage": "https://github.com/kevin-martineze/GlobUI#readme",
|
|
53
|
+
"bugs": {
|
|
54
|
+
"url": "https://github.com/kevin-martineze/GlobUI/issues"
|
|
55
|
+
},
|
|
56
|
+
"publishConfig": {
|
|
57
|
+
"access": "public",
|
|
58
|
+
"provenance": true
|
|
59
|
+
},
|
|
60
|
+
"sideEffects": false,
|
|
61
|
+
"type": "commonjs",
|
|
62
|
+
"engines": {
|
|
63
|
+
"node": ">=18.18"
|
|
64
|
+
},
|
|
65
|
+
"scripts": {
|
|
66
|
+
"build": "tsup",
|
|
67
|
+
"dev": "tsup --watch --no-clean"
|
|
68
|
+
}
|
|
6
69
|
}
|