@glob-ui/accordion 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 +35 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +204 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +177 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +65 -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/accordion
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Sections that open and close. Single or multiple open at once, optionally collapsible.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @glob-ui/accordion
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Documentation
|
|
12
|
+
|
|
13
|
+
Full docs, examples and the accessibility table: [Accordion](https://github.com/kevin-martineze/GlobUI/tree/main/apps/docs/content/docs/components/accordion.mdx).
|
|
14
|
+
|
|
15
|
+
## License
|
|
16
|
+
|
|
17
|
+
MIT. See [LICENSE](./LICENSE).
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
|
|
3
|
+
type BaseProps = Omit<React.HTMLAttributes<HTMLDivElement>, "defaultValue" | "onChange">;
|
|
4
|
+
interface AccordionSingleProps extends BaseProps {
|
|
5
|
+
type?: "single";
|
|
6
|
+
value?: string;
|
|
7
|
+
defaultValue?: string;
|
|
8
|
+
onValueChange?: (value: string) => void;
|
|
9
|
+
/** Allow closing the open item, leaving none open. */
|
|
10
|
+
collapsible?: boolean;
|
|
11
|
+
}
|
|
12
|
+
interface AccordionMultipleProps extends BaseProps {
|
|
13
|
+
type: "multiple";
|
|
14
|
+
value?: string[];
|
|
15
|
+
defaultValue?: string[];
|
|
16
|
+
onValueChange?: (value: string[]) => void;
|
|
17
|
+
collapsible?: never;
|
|
18
|
+
}
|
|
19
|
+
type AccordionProps = AccordionSingleProps | AccordionMultipleProps;
|
|
20
|
+
declare const Accordion: react.ForwardRefExoticComponent<AccordionProps & react.RefAttributes<HTMLDivElement>>;
|
|
21
|
+
interface AccordionItemProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
22
|
+
value: string;
|
|
23
|
+
disabled?: boolean;
|
|
24
|
+
}
|
|
25
|
+
declare const AccordionItem: react.ForwardRefExoticComponent<AccordionItemProps & react.RefAttributes<HTMLDivElement>>;
|
|
26
|
+
type HeadingTag = "h2" | "h3" | "h4" | "h5" | "h6";
|
|
27
|
+
interface AccordionTriggerProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
|
|
28
|
+
/** Heading that wraps the button. Defaults to h3. */
|
|
29
|
+
headingLevel?: HeadingTag;
|
|
30
|
+
}
|
|
31
|
+
declare const AccordionTrigger: react.ForwardRefExoticComponent<AccordionTriggerProps & react.RefAttributes<HTMLButtonElement>>;
|
|
32
|
+
type AccordionContentProps = React.HTMLAttributes<HTMLDivElement>;
|
|
33
|
+
declare const AccordionContent: react.ForwardRefExoticComponent<AccordionContentProps & react.RefAttributes<HTMLDivElement>>;
|
|
34
|
+
|
|
35
|
+
export { Accordion, AccordionContent, type AccordionContentProps, AccordionItem, type AccordionItemProps, type AccordionProps, AccordionTrigger, type AccordionTriggerProps };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
|
|
3
|
+
type BaseProps = Omit<React.HTMLAttributes<HTMLDivElement>, "defaultValue" | "onChange">;
|
|
4
|
+
interface AccordionSingleProps extends BaseProps {
|
|
5
|
+
type?: "single";
|
|
6
|
+
value?: string;
|
|
7
|
+
defaultValue?: string;
|
|
8
|
+
onValueChange?: (value: string) => void;
|
|
9
|
+
/** Allow closing the open item, leaving none open. */
|
|
10
|
+
collapsible?: boolean;
|
|
11
|
+
}
|
|
12
|
+
interface AccordionMultipleProps extends BaseProps {
|
|
13
|
+
type: "multiple";
|
|
14
|
+
value?: string[];
|
|
15
|
+
defaultValue?: string[];
|
|
16
|
+
onValueChange?: (value: string[]) => void;
|
|
17
|
+
collapsible?: never;
|
|
18
|
+
}
|
|
19
|
+
type AccordionProps = AccordionSingleProps | AccordionMultipleProps;
|
|
20
|
+
declare const Accordion: react.ForwardRefExoticComponent<AccordionProps & react.RefAttributes<HTMLDivElement>>;
|
|
21
|
+
interface AccordionItemProps extends React.HTMLAttributes<HTMLDivElement> {
|
|
22
|
+
value: string;
|
|
23
|
+
disabled?: boolean;
|
|
24
|
+
}
|
|
25
|
+
declare const AccordionItem: react.ForwardRefExoticComponent<AccordionItemProps & react.RefAttributes<HTMLDivElement>>;
|
|
26
|
+
type HeadingTag = "h2" | "h3" | "h4" | "h5" | "h6";
|
|
27
|
+
interface AccordionTriggerProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
|
|
28
|
+
/** Heading that wraps the button. Defaults to h3. */
|
|
29
|
+
headingLevel?: HeadingTag;
|
|
30
|
+
}
|
|
31
|
+
declare const AccordionTrigger: react.ForwardRefExoticComponent<AccordionTriggerProps & react.RefAttributes<HTMLButtonElement>>;
|
|
32
|
+
type AccordionContentProps = React.HTMLAttributes<HTMLDivElement>;
|
|
33
|
+
declare const AccordionContent: react.ForwardRefExoticComponent<AccordionContentProps & react.RefAttributes<HTMLDivElement>>;
|
|
34
|
+
|
|
35
|
+
export { Accordion, AccordionContent, type AccordionContentProps, AccordionItem, type AccordionItemProps, type AccordionProps, AccordionTrigger, type AccordionTriggerProps };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
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
|
+
Accordion: () => Accordion,
|
|
54
|
+
AccordionContent: () => AccordionContent,
|
|
55
|
+
AccordionItem: () => AccordionItem,
|
|
56
|
+
AccordionTrigger: () => AccordionTrigger
|
|
57
|
+
});
|
|
58
|
+
module.exports = __toCommonJS(index_exports);
|
|
59
|
+
|
|
60
|
+
// src/accordion.tsx
|
|
61
|
+
var import_react = require("react");
|
|
62
|
+
var import_utils = require("@glob-ui/utils");
|
|
63
|
+
var import_hooks = require("@glob-ui/hooks");
|
|
64
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
65
|
+
var AccordionContext = (0, import_react.createContext)(null);
|
|
66
|
+
var ItemContext = (0, import_react.createContext)(null);
|
|
67
|
+
function useRequired(ctx, part, parent) {
|
|
68
|
+
if (!ctx) throw new Error(`${part} must be used inside ${parent}`);
|
|
69
|
+
return ctx;
|
|
70
|
+
}
|
|
71
|
+
var toArray = (v) => v === void 0 ? [] : Array.isArray(v) ? v : v ? [v] : [];
|
|
72
|
+
var Accordion = (0, import_react.forwardRef)((props, ref) => {
|
|
73
|
+
const _a = props, { type = "single", value, defaultValue, onValueChange, collapsible = false, className, onKeyDown } = _a, rest = __objRest(_a, ["type", "value", "defaultValue", "onValueChange", "collapsible", "className", "onKeyDown"]);
|
|
74
|
+
const [internal, setInternal] = (0, import_react.useState)(() => toArray(defaultValue));
|
|
75
|
+
const isControlled = value !== void 0;
|
|
76
|
+
const openValues = isControlled ? toArray(value) : internal;
|
|
77
|
+
function toggle(item) {
|
|
78
|
+
var _a2;
|
|
79
|
+
const isOpen = openValues.includes(item);
|
|
80
|
+
let next;
|
|
81
|
+
if (type === "multiple") next = isOpen ? openValues.filter((v) => v !== item) : [...openValues, item];
|
|
82
|
+
else if (isOpen) {
|
|
83
|
+
if (!collapsible) return;
|
|
84
|
+
next = [];
|
|
85
|
+
} else next = [item];
|
|
86
|
+
if (!isControlled) setInternal(next);
|
|
87
|
+
const emit = onValueChange;
|
|
88
|
+
emit == null ? void 0 : emit(type === "multiple" ? next : (_a2 = next[0]) != null ? _a2 : "");
|
|
89
|
+
}
|
|
90
|
+
const roving = (0, import_hooks.useRovingFocus)({ orientation: "vertical", loop: true, itemSelector: "[data-accordion-trigger]:not([disabled])" });
|
|
91
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AccordionContext.Provider, { value: { isOpen: (v) => openValues.includes(v), toggle }, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
92
|
+
"div",
|
|
93
|
+
__spreadValues(__spreadProps(__spreadValues({
|
|
94
|
+
ref,
|
|
95
|
+
className: (0, import_utils.cn)("flex flex-col", className)
|
|
96
|
+
}, roving.rootProps), {
|
|
97
|
+
onKeyDown: (e) => {
|
|
98
|
+
onKeyDown == null ? void 0 : onKeyDown(e);
|
|
99
|
+
roving.onKeyDown(e);
|
|
100
|
+
}
|
|
101
|
+
}), rest)
|
|
102
|
+
) });
|
|
103
|
+
});
|
|
104
|
+
Accordion.displayName = "Accordion";
|
|
105
|
+
var AccordionItem = (0, import_react.forwardRef)(
|
|
106
|
+
(_a, ref) => {
|
|
107
|
+
var _b = _a, { value, disabled = false, className } = _b, props = __objRest(_b, ["value", "disabled", "className"]);
|
|
108
|
+
const ctx = useRequired((0, import_react.useContext)(AccordionContext), "AccordionItem", "Accordion");
|
|
109
|
+
const baseId = (0, import_react.useId)();
|
|
110
|
+
const open = ctx.isOpen(value);
|
|
111
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
112
|
+
ItemContext.Provider,
|
|
113
|
+
{
|
|
114
|
+
value: { value, open, disabled, triggerId: `${baseId}-trigger`, contentId: `${baseId}-content` },
|
|
115
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
116
|
+
"div",
|
|
117
|
+
__spreadValues({
|
|
118
|
+
ref,
|
|
119
|
+
"data-state": open ? "open" : "closed",
|
|
120
|
+
className: (0, import_utils.cn)("border-b border-glob-border", className)
|
|
121
|
+
}, props)
|
|
122
|
+
)
|
|
123
|
+
}
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
);
|
|
127
|
+
AccordionItem.displayName = "AccordionItem";
|
|
128
|
+
var AccordionTrigger = (0, import_react.forwardRef)(
|
|
129
|
+
(_a, ref) => {
|
|
130
|
+
var _b = _a, { headingLevel: Heading = "h3", className, children, onClick } = _b, props = __objRest(_b, ["headingLevel", "className", "children", "onClick"]);
|
|
131
|
+
const ctx = useRequired((0, import_react.useContext)(AccordionContext), "AccordionTrigger", "Accordion");
|
|
132
|
+
const item = useRequired((0, import_react.useContext)(ItemContext), "AccordionTrigger", "AccordionItem");
|
|
133
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Heading, { className: "flex", children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
|
|
134
|
+
"button",
|
|
135
|
+
__spreadProps(__spreadValues({
|
|
136
|
+
ref,
|
|
137
|
+
type: "button",
|
|
138
|
+
id: item.triggerId,
|
|
139
|
+
"aria-expanded": item.open,
|
|
140
|
+
"aria-controls": item.contentId,
|
|
141
|
+
disabled: item.disabled,
|
|
142
|
+
"data-accordion-trigger": "",
|
|
143
|
+
"data-state": item.open ? "open" : "closed",
|
|
144
|
+
className: (0, import_utils.cn)(
|
|
145
|
+
"group flex flex-1 items-center justify-between gap-4 py-4 text-left text-sm font-medium text-glob-text transition-colors hover:text-glob-primary focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-glob-ring disabled:cursor-not-allowed disabled:opacity-50",
|
|
146
|
+
className
|
|
147
|
+
),
|
|
148
|
+
onClick: (e) => {
|
|
149
|
+
onClick == null ? void 0 : onClick(e);
|
|
150
|
+
if (!e.defaultPrevented) ctx.toggle(item.value);
|
|
151
|
+
}
|
|
152
|
+
}, props), {
|
|
153
|
+
children: [
|
|
154
|
+
children,
|
|
155
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
156
|
+
"svg",
|
|
157
|
+
{
|
|
158
|
+
"aria-hidden": "true",
|
|
159
|
+
viewBox: "0 0 24 24",
|
|
160
|
+
fill: "none",
|
|
161
|
+
stroke: "currentColor",
|
|
162
|
+
strokeWidth: "2",
|
|
163
|
+
className: "h-4 w-4 shrink-0 text-glob-text-secondary transition-transform duration-200 group-data-[state=open]:rotate-180 motion-reduce:transition-none",
|
|
164
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("path", { d: "M6 9l6 6 6-6" })
|
|
165
|
+
}
|
|
166
|
+
)
|
|
167
|
+
]
|
|
168
|
+
})
|
|
169
|
+
) });
|
|
170
|
+
}
|
|
171
|
+
);
|
|
172
|
+
AccordionTrigger.displayName = "AccordionTrigger";
|
|
173
|
+
var AccordionContent = (0, import_react.forwardRef)(
|
|
174
|
+
(_a, ref) => {
|
|
175
|
+
var _b = _a, { className, children } = _b, props = __objRest(_b, ["className", "children"]);
|
|
176
|
+
const item = useRequired((0, import_react.useContext)(ItemContext), "AccordionContent", "AccordionItem");
|
|
177
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
178
|
+
"div",
|
|
179
|
+
__spreadProps(__spreadValues({
|
|
180
|
+
ref,
|
|
181
|
+
id: item.contentId,
|
|
182
|
+
role: "region",
|
|
183
|
+
"aria-labelledby": item.triggerId,
|
|
184
|
+
"data-state": item.open ? "open" : "closed",
|
|
185
|
+
inert: !item.open,
|
|
186
|
+
className: (0, import_utils.cn)(
|
|
187
|
+
"grid transition-[grid-template-rows] duration-200 ease-out motion-reduce:transition-none",
|
|
188
|
+
item.open ? "grid-rows-[1fr]" : "grid-rows-[0fr]"
|
|
189
|
+
)
|
|
190
|
+
}, props), {
|
|
191
|
+
children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "overflow-hidden", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: (0, import_utils.cn)("pb-4 text-sm text-glob-text-secondary", className), children }) })
|
|
192
|
+
})
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
);
|
|
196
|
+
AccordionContent.displayName = "AccordionContent";
|
|
197
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
198
|
+
0 && (module.exports = {
|
|
199
|
+
Accordion,
|
|
200
|
+
AccordionContent,
|
|
201
|
+
AccordionItem,
|
|
202
|
+
AccordionTrigger
|
|
203
|
+
});
|
|
204
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/accordion.tsx"],"sourcesContent":["export { Accordion, AccordionItem, AccordionTrigger, AccordionContent } from \"./accordion\"\nexport type { AccordionProps, AccordionItemProps, AccordionTriggerProps, AccordionContentProps } from \"./accordion\"\n","\"use client\"\n\nimport { createContext, forwardRef, useContext, useId, useState } from \"react\"\nimport { cn } from \"@glob-ui/utils\"\nimport { useRovingFocus } from \"@glob-ui/hooks\"\n\ninterface AccordionContextValue {\n isOpen: (value: string) => boolean\n toggle: (value: string) => void\n}\n\nconst AccordionContext = createContext<AccordionContextValue | null>(null)\n\ninterface ItemContextValue {\n value: string\n open: boolean\n disabled: boolean\n triggerId: string\n contentId: string\n}\n\nconst ItemContext = createContext<ItemContextValue | null>(null)\n\nfunction useRequired<T>(ctx: T | null, part: string, parent: string): T {\n if (!ctx) throw new Error(`${part} must be used inside ${parent}`)\n return ctx\n}\n\ntype BaseProps = Omit<React.HTMLAttributes<HTMLDivElement>, \"defaultValue\" | \"onChange\">\n\ninterface AccordionSingleProps extends BaseProps {\n type?: \"single\"\n value?: string\n defaultValue?: string\n onValueChange?: (value: string) => void\n /** Allow closing the open item, leaving none open. */\n collapsible?: boolean\n}\n\ninterface AccordionMultipleProps extends BaseProps {\n type: \"multiple\"\n value?: string[]\n defaultValue?: string[]\n onValueChange?: (value: string[]) => void\n collapsible?: never\n}\n\nexport type AccordionProps = AccordionSingleProps | AccordionMultipleProps\n\nconst toArray = (v: string | string[] | undefined) => (v === undefined ? [] : Array.isArray(v) ? v : v ? [v] : [])\n\nconst Accordion = forwardRef<HTMLDivElement, AccordionProps>((props, ref) => {\n const { type = \"single\", value, defaultValue, onValueChange, collapsible = false, className, onKeyDown, ...rest } =\n props as BaseProps & {\n type?: \"single\" | \"multiple\"\n value?: string | string[]\n defaultValue?: string | string[]\n onValueChange?: (value: never) => void\n collapsible?: boolean\n }\n\n const [internal, setInternal] = useState<string[]>(() => toArray(defaultValue))\n const isControlled = value !== undefined\n const openValues = isControlled ? toArray(value) : internal\n\n function toggle(item: string) {\n const isOpen = openValues.includes(item)\n let next: string[]\n if (type === \"multiple\") next = isOpen ? openValues.filter((v) => v !== item) : [...openValues, item]\n else if (isOpen) {\n if (!collapsible) return\n next = []\n } else next = [item]\n\n if (!isControlled) setInternal(next)\n const emit = onValueChange as ((v: string | string[]) => void) | undefined\n emit?.(type === \"multiple\" ? next : (next[0] ?? \"\"))\n }\n\n // Optional APG keys: move between headers with arrows / Home / End.\n const roving = useRovingFocus({ orientation: \"vertical\", loop: true, itemSelector: \"[data-accordion-trigger]:not([disabled])\" })\n\n return (\n <AccordionContext.Provider value={{ isOpen: (v) => openValues.includes(v), toggle }}>\n <div\n ref={ref}\n className={cn(\"flex flex-col\", className)}\n {...roving.rootProps}\n onKeyDown={(e) => {\n onKeyDown?.(e)\n roving.onKeyDown(e)\n }}\n {...rest}\n />\n </AccordionContext.Provider>\n )\n})\nAccordion.displayName = \"Accordion\"\n\nexport interface AccordionItemProps extends React.HTMLAttributes<HTMLDivElement> {\n value: string\n disabled?: boolean\n}\n\nconst AccordionItem = forwardRef<HTMLDivElement, AccordionItemProps>(\n ({ value, disabled = false, className, ...props }, ref) => {\n const ctx = useRequired(useContext(AccordionContext), \"AccordionItem\", \"Accordion\")\n const baseId = useId()\n const open = ctx.isOpen(value)\n return (\n <ItemContext.Provider\n value={{ value, open, disabled, triggerId: `${baseId}-trigger`, contentId: `${baseId}-content` }}\n >\n <div\n ref={ref}\n data-state={open ? \"open\" : \"closed\"}\n className={cn(\"border-b border-glob-border\", className)}\n {...props}\n />\n </ItemContext.Provider>\n )\n }\n)\nAccordionItem.displayName = \"AccordionItem\"\n\ntype HeadingTag = \"h2\" | \"h3\" | \"h4\" | \"h5\" | \"h6\"\n\nexport interface AccordionTriggerProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {\n /** Heading that wraps the button. Defaults to h3. */\n headingLevel?: HeadingTag\n}\n\nconst AccordionTrigger = forwardRef<HTMLButtonElement, AccordionTriggerProps>(\n ({ headingLevel: Heading = \"h3\", className, children, onClick, ...props }, ref) => {\n const ctx = useRequired(useContext(AccordionContext), \"AccordionTrigger\", \"Accordion\")\n const item = useRequired(useContext(ItemContext), \"AccordionTrigger\", \"AccordionItem\")\n return (\n <Heading className=\"flex\">\n <button\n ref={ref}\n type=\"button\"\n id={item.triggerId}\n aria-expanded={item.open}\n aria-controls={item.contentId}\n disabled={item.disabled}\n data-accordion-trigger=\"\"\n data-state={item.open ? \"open\" : \"closed\"}\n className={cn(\n \"group flex flex-1 items-center justify-between gap-4 py-4 text-left text-sm font-medium text-glob-text transition-colors hover:text-glob-primary focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-glob-ring disabled:cursor-not-allowed disabled:opacity-50\",\n className\n )}\n onClick={(e) => {\n onClick?.(e)\n if (!e.defaultPrevented) ctx.toggle(item.value)\n }}\n {...props}\n >\n {children}\n <svg\n aria-hidden=\"true\"\n viewBox=\"0 0 24 24\"\n fill=\"none\"\n stroke=\"currentColor\"\n strokeWidth=\"2\"\n className=\"h-4 w-4 shrink-0 text-glob-text-secondary transition-transform duration-200 group-data-[state=open]:rotate-180 motion-reduce:transition-none\"\n >\n <path d=\"M6 9l6 6 6-6\" />\n </svg>\n </button>\n </Heading>\n )\n }\n)\nAccordionTrigger.displayName = \"AccordionTrigger\"\n\nexport type AccordionContentProps = React.HTMLAttributes<HTMLDivElement>\n\nconst AccordionContent = forwardRef<HTMLDivElement, AccordionContentProps>(\n ({ className, children, ...props }, ref) => {\n const item = useRequired(useContext(ItemContext), \"AccordionContent\", \"AccordionItem\")\n return (\n <div\n ref={ref}\n id={item.contentId}\n role=\"region\"\n aria-labelledby={item.triggerId}\n data-state={item.open ? \"open\" : \"closed\"}\n // Closed content stays mounted for the animation; inert keeps it out of focus and the a11y tree.\n inert={!item.open}\n className={cn(\n \"grid transition-[grid-template-rows] duration-200 ease-out motion-reduce:transition-none\",\n item.open ? \"grid-rows-[1fr]\" : \"grid-rows-[0fr]\"\n )}\n {...props}\n >\n <div className=\"overflow-hidden\">\n <div className={cn(\"pb-4 text-sm text-glob-text-secondary\", className)}>{children}</div>\n </div>\n </div>\n )\n }\n)\nAccordionContent.displayName = \"AccordionContent\"\n\nexport { Accordion, AccordionItem, AccordionTrigger, AccordionContent }\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEA,mBAAuE;AACvE,mBAAmB;AACnB,mBAA+B;AAgFzB;AAzEN,IAAM,uBAAmB,4BAA4C,IAAI;AAUzE,IAAM,kBAAc,4BAAuC,IAAI;AAE/D,SAAS,YAAe,KAAe,MAAc,QAAmB;AACtE,MAAI,CAAC,IAAK,OAAM,IAAI,MAAM,GAAG,IAAI,wBAAwB,MAAM,EAAE;AACjE,SAAO;AACT;AAuBA,IAAM,UAAU,CAAC,MAAsC,MAAM,SAAY,CAAC,IAAI,MAAM,QAAQ,CAAC,IAAI,IAAI,IAAI,CAAC,CAAC,IAAI,CAAC;AAEhH,IAAM,gBAAY,yBAA2C,CAAC,OAAO,QAAQ;AAC3E,QACE,YADM,SAAO,UAAU,OAAO,cAAc,eAAe,cAAc,OAAO,WAAW,UApD/F,IAqDI,IADyG,iBACzG,IADyG,CAAnG,QAAiB,SAAO,gBAAc,iBAAe,eAAqB,aAAW;AAS7F,QAAM,CAAC,UAAU,WAAW,QAAI,uBAAmB,MAAM,QAAQ,YAAY,CAAC;AAC9E,QAAM,eAAe,UAAU;AAC/B,QAAM,aAAa,eAAe,QAAQ,KAAK,IAAI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AE,aAAW,SAlLhB,IAkLG,IAA0B,kBAA1B,IAA0B,CAAxB,aAAW;AACZ,UAAM,OAAO,gBAAY,yBAAW,WAAW,GAAG,oBAAoB,eAAe;AACrF,WACE;AAAA,MAAC;AAAA;AAAA,QACC;AAAA,QACA,IAAI,KAAK;AAAA,QACT,MAAK;AAAA,QACL,mBAAiB,KAAK;AAAA,QACtB,cAAY,KAAK,OAAO,SAAS;AAAA,QAEjC,OAAO,CAAC,KAAK;AAAA,QACb,eAAW;AAAA,UACT;AAAA,UACA,KAAK,OAAO,oBAAoB;AAAA,QAClC;AAAA,SACI,QAZL;AAAA,QAcC,sDAAC,SAAI,WAAU,mBACb,sDAAC,SAAI,eAAW,iBAAG,yCAAyC,SAAS,GAAI,UAAS,GACpF;AAAA;AAAA,IACF;AAAA,EAEJ;AACF;AACA,iBAAiB,cAAc;","names":["_a"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
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/accordion.tsx
|
|
35
|
+
import { createContext, forwardRef, useContext, useId, useState } from "react";
|
|
36
|
+
import { cn } from "@glob-ui/utils";
|
|
37
|
+
import { useRovingFocus } from "@glob-ui/hooks";
|
|
38
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
39
|
+
var AccordionContext = createContext(null);
|
|
40
|
+
var ItemContext = createContext(null);
|
|
41
|
+
function useRequired(ctx, part, parent) {
|
|
42
|
+
if (!ctx) throw new Error(`${part} must be used inside ${parent}`);
|
|
43
|
+
return ctx;
|
|
44
|
+
}
|
|
45
|
+
var toArray = (v) => v === void 0 ? [] : Array.isArray(v) ? v : v ? [v] : [];
|
|
46
|
+
var Accordion = forwardRef((props, ref) => {
|
|
47
|
+
const _a = props, { type = "single", value, defaultValue, onValueChange, collapsible = false, className, onKeyDown } = _a, rest = __objRest(_a, ["type", "value", "defaultValue", "onValueChange", "collapsible", "className", "onKeyDown"]);
|
|
48
|
+
const [internal, setInternal] = useState(() => toArray(defaultValue));
|
|
49
|
+
const isControlled = value !== void 0;
|
|
50
|
+
const openValues = isControlled ? toArray(value) : internal;
|
|
51
|
+
function toggle(item) {
|
|
52
|
+
var _a2;
|
|
53
|
+
const isOpen = openValues.includes(item);
|
|
54
|
+
let next;
|
|
55
|
+
if (type === "multiple") next = isOpen ? openValues.filter((v) => v !== item) : [...openValues, item];
|
|
56
|
+
else if (isOpen) {
|
|
57
|
+
if (!collapsible) return;
|
|
58
|
+
next = [];
|
|
59
|
+
} else next = [item];
|
|
60
|
+
if (!isControlled) setInternal(next);
|
|
61
|
+
const emit = onValueChange;
|
|
62
|
+
emit == null ? void 0 : emit(type === "multiple" ? next : (_a2 = next[0]) != null ? _a2 : "");
|
|
63
|
+
}
|
|
64
|
+
const roving = useRovingFocus({ orientation: "vertical", loop: true, itemSelector: "[data-accordion-trigger]:not([disabled])" });
|
|
65
|
+
return /* @__PURE__ */ jsx(AccordionContext.Provider, { value: { isOpen: (v) => openValues.includes(v), toggle }, children: /* @__PURE__ */ jsx(
|
|
66
|
+
"div",
|
|
67
|
+
__spreadValues(__spreadProps(__spreadValues({
|
|
68
|
+
ref,
|
|
69
|
+
className: cn("flex flex-col", className)
|
|
70
|
+
}, roving.rootProps), {
|
|
71
|
+
onKeyDown: (e) => {
|
|
72
|
+
onKeyDown == null ? void 0 : onKeyDown(e);
|
|
73
|
+
roving.onKeyDown(e);
|
|
74
|
+
}
|
|
75
|
+
}), rest)
|
|
76
|
+
) });
|
|
77
|
+
});
|
|
78
|
+
Accordion.displayName = "Accordion";
|
|
79
|
+
var AccordionItem = forwardRef(
|
|
80
|
+
(_a, ref) => {
|
|
81
|
+
var _b = _a, { value, disabled = false, className } = _b, props = __objRest(_b, ["value", "disabled", "className"]);
|
|
82
|
+
const ctx = useRequired(useContext(AccordionContext), "AccordionItem", "Accordion");
|
|
83
|
+
const baseId = useId();
|
|
84
|
+
const open = ctx.isOpen(value);
|
|
85
|
+
return /* @__PURE__ */ jsx(
|
|
86
|
+
ItemContext.Provider,
|
|
87
|
+
{
|
|
88
|
+
value: { value, open, disabled, triggerId: `${baseId}-trigger`, contentId: `${baseId}-content` },
|
|
89
|
+
children: /* @__PURE__ */ jsx(
|
|
90
|
+
"div",
|
|
91
|
+
__spreadValues({
|
|
92
|
+
ref,
|
|
93
|
+
"data-state": open ? "open" : "closed",
|
|
94
|
+
className: cn("border-b border-glob-border", className)
|
|
95
|
+
}, props)
|
|
96
|
+
)
|
|
97
|
+
}
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
);
|
|
101
|
+
AccordionItem.displayName = "AccordionItem";
|
|
102
|
+
var AccordionTrigger = forwardRef(
|
|
103
|
+
(_a, ref) => {
|
|
104
|
+
var _b = _a, { headingLevel: Heading = "h3", className, children, onClick } = _b, props = __objRest(_b, ["headingLevel", "className", "children", "onClick"]);
|
|
105
|
+
const ctx = useRequired(useContext(AccordionContext), "AccordionTrigger", "Accordion");
|
|
106
|
+
const item = useRequired(useContext(ItemContext), "AccordionTrigger", "AccordionItem");
|
|
107
|
+
return /* @__PURE__ */ jsx(Heading, { className: "flex", children: /* @__PURE__ */ jsxs(
|
|
108
|
+
"button",
|
|
109
|
+
__spreadProps(__spreadValues({
|
|
110
|
+
ref,
|
|
111
|
+
type: "button",
|
|
112
|
+
id: item.triggerId,
|
|
113
|
+
"aria-expanded": item.open,
|
|
114
|
+
"aria-controls": item.contentId,
|
|
115
|
+
disabled: item.disabled,
|
|
116
|
+
"data-accordion-trigger": "",
|
|
117
|
+
"data-state": item.open ? "open" : "closed",
|
|
118
|
+
className: cn(
|
|
119
|
+
"group flex flex-1 items-center justify-between gap-4 py-4 text-left text-sm font-medium text-glob-text transition-colors hover:text-glob-primary focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-glob-ring disabled:cursor-not-allowed disabled:opacity-50",
|
|
120
|
+
className
|
|
121
|
+
),
|
|
122
|
+
onClick: (e) => {
|
|
123
|
+
onClick == null ? void 0 : onClick(e);
|
|
124
|
+
if (!e.defaultPrevented) ctx.toggle(item.value);
|
|
125
|
+
}
|
|
126
|
+
}, props), {
|
|
127
|
+
children: [
|
|
128
|
+
children,
|
|
129
|
+
/* @__PURE__ */ jsx(
|
|
130
|
+
"svg",
|
|
131
|
+
{
|
|
132
|
+
"aria-hidden": "true",
|
|
133
|
+
viewBox: "0 0 24 24",
|
|
134
|
+
fill: "none",
|
|
135
|
+
stroke: "currentColor",
|
|
136
|
+
strokeWidth: "2",
|
|
137
|
+
className: "h-4 w-4 shrink-0 text-glob-text-secondary transition-transform duration-200 group-data-[state=open]:rotate-180 motion-reduce:transition-none",
|
|
138
|
+
children: /* @__PURE__ */ jsx("path", { d: "M6 9l6 6 6-6" })
|
|
139
|
+
}
|
|
140
|
+
)
|
|
141
|
+
]
|
|
142
|
+
})
|
|
143
|
+
) });
|
|
144
|
+
}
|
|
145
|
+
);
|
|
146
|
+
AccordionTrigger.displayName = "AccordionTrigger";
|
|
147
|
+
var AccordionContent = forwardRef(
|
|
148
|
+
(_a, ref) => {
|
|
149
|
+
var _b = _a, { className, children } = _b, props = __objRest(_b, ["className", "children"]);
|
|
150
|
+
const item = useRequired(useContext(ItemContext), "AccordionContent", "AccordionItem");
|
|
151
|
+
return /* @__PURE__ */ jsx(
|
|
152
|
+
"div",
|
|
153
|
+
__spreadProps(__spreadValues({
|
|
154
|
+
ref,
|
|
155
|
+
id: item.contentId,
|
|
156
|
+
role: "region",
|
|
157
|
+
"aria-labelledby": item.triggerId,
|
|
158
|
+
"data-state": item.open ? "open" : "closed",
|
|
159
|
+
inert: !item.open,
|
|
160
|
+
className: cn(
|
|
161
|
+
"grid transition-[grid-template-rows] duration-200 ease-out motion-reduce:transition-none",
|
|
162
|
+
item.open ? "grid-rows-[1fr]" : "grid-rows-[0fr]"
|
|
163
|
+
)
|
|
164
|
+
}, props), {
|
|
165
|
+
children: /* @__PURE__ */ jsx("div", { className: "overflow-hidden", children: /* @__PURE__ */ jsx("div", { className: cn("pb-4 text-sm text-glob-text-secondary", className), children }) })
|
|
166
|
+
})
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
);
|
|
170
|
+
AccordionContent.displayName = "AccordionContent";
|
|
171
|
+
export {
|
|
172
|
+
Accordion,
|
|
173
|
+
AccordionContent,
|
|
174
|
+
AccordionItem,
|
|
175
|
+
AccordionTrigger
|
|
176
|
+
};
|
|
177
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/accordion.tsx"],"sourcesContent":["\"use client\"\n\nimport { createContext, forwardRef, useContext, useId, useState } from \"react\"\nimport { cn } from \"@glob-ui/utils\"\nimport { useRovingFocus } from \"@glob-ui/hooks\"\n\ninterface AccordionContextValue {\n isOpen: (value: string) => boolean\n toggle: (value: string) => void\n}\n\nconst AccordionContext = createContext<AccordionContextValue | null>(null)\n\ninterface ItemContextValue {\n value: string\n open: boolean\n disabled: boolean\n triggerId: string\n contentId: string\n}\n\nconst ItemContext = createContext<ItemContextValue | null>(null)\n\nfunction useRequired<T>(ctx: T | null, part: string, parent: string): T {\n if (!ctx) throw new Error(`${part} must be used inside ${parent}`)\n return ctx\n}\n\ntype BaseProps = Omit<React.HTMLAttributes<HTMLDivElement>, \"defaultValue\" | \"onChange\">\n\ninterface AccordionSingleProps extends BaseProps {\n type?: \"single\"\n value?: string\n defaultValue?: string\n onValueChange?: (value: string) => void\n /** Allow closing the open item, leaving none open. */\n collapsible?: boolean\n}\n\ninterface AccordionMultipleProps extends BaseProps {\n type: \"multiple\"\n value?: string[]\n defaultValue?: string[]\n onValueChange?: (value: string[]) => void\n collapsible?: never\n}\n\nexport type AccordionProps = AccordionSingleProps | AccordionMultipleProps\n\nconst toArray = (v: string | string[] | undefined) => (v === undefined ? [] : Array.isArray(v) ? v : v ? [v] : [])\n\nconst Accordion = forwardRef<HTMLDivElement, AccordionProps>((props, ref) => {\n const { type = \"single\", value, defaultValue, onValueChange, collapsible = false, className, onKeyDown, ...rest } =\n props as BaseProps & {\n type?: \"single\" | \"multiple\"\n value?: string | string[]\n defaultValue?: string | string[]\n onValueChange?: (value: never) => void\n collapsible?: boolean\n }\n\n const [internal, setInternal] = useState<string[]>(() => toArray(defaultValue))\n const isControlled = value !== undefined\n const openValues = isControlled ? toArray(value) : internal\n\n function toggle(item: string) {\n const isOpen = openValues.includes(item)\n let next: string[]\n if (type === \"multiple\") next = isOpen ? openValues.filter((v) => v !== item) : [...openValues, item]\n else if (isOpen) {\n if (!collapsible) return\n next = []\n } else next = [item]\n\n if (!isControlled) setInternal(next)\n const emit = onValueChange as ((v: string | string[]) => void) | undefined\n emit?.(type === \"multiple\" ? next : (next[0] ?? \"\"))\n }\n\n // Optional APG keys: move between headers with arrows / Home / End.\n const roving = useRovingFocus({ orientation: \"vertical\", loop: true, itemSelector: \"[data-accordion-trigger]:not([disabled])\" })\n\n return (\n <AccordionContext.Provider value={{ isOpen: (v) => openValues.includes(v), toggle }}>\n <div\n ref={ref}\n className={cn(\"flex flex-col\", className)}\n {...roving.rootProps}\n onKeyDown={(e) => {\n onKeyDown?.(e)\n roving.onKeyDown(e)\n }}\n {...rest}\n />\n </AccordionContext.Provider>\n )\n})\nAccordion.displayName = \"Accordion\"\n\nexport interface AccordionItemProps extends React.HTMLAttributes<HTMLDivElement> {\n value: string\n disabled?: boolean\n}\n\nconst AccordionItem = forwardRef<HTMLDivElement, AccordionItemProps>(\n ({ value, disabled = false, className, ...props }, ref) => {\n const ctx = useRequired(useContext(AccordionContext), \"AccordionItem\", \"Accordion\")\n const baseId = useId()\n const open = ctx.isOpen(value)\n return (\n <ItemContext.Provider\n value={{ value, open, disabled, triggerId: `${baseId}-trigger`, contentId: `${baseId}-content` }}\n >\n <div\n ref={ref}\n data-state={open ? \"open\" : \"closed\"}\n className={cn(\"border-b border-glob-border\", className)}\n {...props}\n />\n </ItemContext.Provider>\n )\n }\n)\nAccordionItem.displayName = \"AccordionItem\"\n\ntype HeadingTag = \"h2\" | \"h3\" | \"h4\" | \"h5\" | \"h6\"\n\nexport interface AccordionTriggerProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {\n /** Heading that wraps the button. Defaults to h3. */\n headingLevel?: HeadingTag\n}\n\nconst AccordionTrigger = forwardRef<HTMLButtonElement, AccordionTriggerProps>(\n ({ headingLevel: Heading = \"h3\", className, children, onClick, ...props }, ref) => {\n const ctx = useRequired(useContext(AccordionContext), \"AccordionTrigger\", \"Accordion\")\n const item = useRequired(useContext(ItemContext), \"AccordionTrigger\", \"AccordionItem\")\n return (\n <Heading className=\"flex\">\n <button\n ref={ref}\n type=\"button\"\n id={item.triggerId}\n aria-expanded={item.open}\n aria-controls={item.contentId}\n disabled={item.disabled}\n data-accordion-trigger=\"\"\n data-state={item.open ? \"open\" : \"closed\"}\n className={cn(\n \"group flex flex-1 items-center justify-between gap-4 py-4 text-left text-sm font-medium text-glob-text transition-colors hover:text-glob-primary focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-glob-ring disabled:cursor-not-allowed disabled:opacity-50\",\n className\n )}\n onClick={(e) => {\n onClick?.(e)\n if (!e.defaultPrevented) ctx.toggle(item.value)\n }}\n {...props}\n >\n {children}\n <svg\n aria-hidden=\"true\"\n viewBox=\"0 0 24 24\"\n fill=\"none\"\n stroke=\"currentColor\"\n strokeWidth=\"2\"\n className=\"h-4 w-4 shrink-0 text-glob-text-secondary transition-transform duration-200 group-data-[state=open]:rotate-180 motion-reduce:transition-none\"\n >\n <path d=\"M6 9l6 6 6-6\" />\n </svg>\n </button>\n </Heading>\n )\n }\n)\nAccordionTrigger.displayName = \"AccordionTrigger\"\n\nexport type AccordionContentProps = React.HTMLAttributes<HTMLDivElement>\n\nconst AccordionContent = forwardRef<HTMLDivElement, AccordionContentProps>(\n ({ className, children, ...props }, ref) => {\n const item = useRequired(useContext(ItemContext), \"AccordionContent\", \"AccordionItem\")\n return (\n <div\n ref={ref}\n id={item.contentId}\n role=\"region\"\n aria-labelledby={item.triggerId}\n data-state={item.open ? \"open\" : \"closed\"}\n // Closed content stays mounted for the animation; inert keeps it out of focus and the a11y tree.\n inert={!item.open}\n className={cn(\n \"grid transition-[grid-template-rows] duration-200 ease-out motion-reduce:transition-none\",\n item.open ? \"grid-rows-[1fr]\" : \"grid-rows-[0fr]\"\n )}\n {...props}\n >\n <div className=\"overflow-hidden\">\n <div className={cn(\"pb-4 text-sm text-glob-text-secondary\", className)}>{children}</div>\n </div>\n </div>\n )\n }\n)\nAccordionContent.displayName = \"AccordionContent\"\n\nexport { Accordion, AccordionItem, AccordionTrigger, AccordionContent }\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,SAAS,eAAe,YAAY,YAAY,OAAO,gBAAgB;AACvE,SAAS,UAAU;AACnB,SAAS,sBAAsB;AAgFzB,cAsDE,YAtDF;AAzEN,IAAM,mBAAmB,cAA4C,IAAI;AAUzE,IAAM,cAAc,cAAuC,IAAI;AAE/D,SAAS,YAAe,KAAe,MAAc,QAAmB;AACtE,MAAI,CAAC,IAAK,OAAM,IAAI,MAAM,GAAG,IAAI,wBAAwB,MAAM,EAAE;AACjE,SAAO;AACT;AAuBA,IAAM,UAAU,CAAC,MAAsC,MAAM,SAAY,CAAC,IAAI,MAAM,QAAQ,CAAC,IAAI,IAAI,IAAI,CAAC,CAAC,IAAI,CAAC;AAEhH,IAAM,YAAY,WAA2C,CAAC,OAAO,QAAQ;AAC3E,QACE,YADM,SAAO,UAAU,OAAO,cAAc,eAAe,cAAc,OAAO,WAAW,UApD/F,IAqDI,IADyG,iBACzG,IADyG,CAAnG,QAAiB,SAAO,gBAAc,iBAAe,eAAqB,aAAW;AAS7F,QAAM,CAAC,UAAU,WAAW,IAAI,SAAmB,MAAM,QAAQ,YAAY,CAAC;AAC9E,QAAM,eAAe,UAAU;AAC/B,QAAM,aAAa,eAAe,QAAQ,KAAK,IAAI;AAEnD,WAAS,OAAO,MAAc;AAjEhC,QAAAA;AAkEI,UAAM,SAAS,WAAW,SAAS,IAAI;AACvC,QAAI;AACJ,QAAI,SAAS,WAAY,QAAO,SAAS,WAAW,OAAO,CAAC,MAAM,MAAM,IAAI,IAAI,CAAC,GAAG,YAAY,IAAI;AAAA,aAC3F,QAAQ;AACf,UAAI,CAAC,YAAa;AAClB,aAAO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AAA,QAEjC,OAAO,CAAC,KAAK;AAAA,QACb,WAAW;AAAA,UACT;AAAA,UACA,KAAK,OAAO,oBAAoB;AAAA,QAClC;AAAA,SACI,QAZL;AAAA,QAcC,8BAAC,SAAI,WAAU,mBACb,8BAAC,SAAI,WAAW,GAAG,yCAAyC,SAAS,GAAI,UAAS,GACpF;AAAA;AAAA,IACF;AAAA,EAEJ;AACF;AACA,iBAAiB,cAAc;","names":["_a"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,68 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@glob-ui/accordion",
|
|
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
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"tsup": "^8.5.1"
|
|
34
|
+
},
|
|
35
|
+
"description": "Sections that open and close. Single or multiple open at once, optionally collapsible.",
|
|
36
|
+
"keywords": [
|
|
37
|
+
"react",
|
|
38
|
+
"glob-ui",
|
|
39
|
+
"components",
|
|
40
|
+
"accessible",
|
|
41
|
+
"tailwind",
|
|
42
|
+
"accordion"
|
|
43
|
+
],
|
|
44
|
+
"license": "MIT",
|
|
45
|
+
"author": "Kevin Martínez E",
|
|
46
|
+
"repository": {
|
|
47
|
+
"type": "git",
|
|
48
|
+
"url": "git+https://github.com/kevin-martineze/GlobUI.git",
|
|
49
|
+
"directory": "packages/accordion"
|
|
50
|
+
},
|
|
51
|
+
"homepage": "https://github.com/kevin-martineze/GlobUI#readme",
|
|
52
|
+
"bugs": {
|
|
53
|
+
"url": "https://github.com/kevin-martineze/GlobUI/issues"
|
|
54
|
+
},
|
|
55
|
+
"publishConfig": {
|
|
56
|
+
"access": "public",
|
|
57
|
+
"provenance": true
|
|
58
|
+
},
|
|
59
|
+
"sideEffects": false,
|
|
60
|
+
"type": "commonjs",
|
|
61
|
+
"engines": {
|
|
62
|
+
"node": ">=18.18"
|
|
63
|
+
},
|
|
64
|
+
"scripts": {
|
|
65
|
+
"build": "tsup",
|
|
66
|
+
"dev": "tsup --watch --no-clean"
|
|
67
|
+
}
|
|
6
68
|
}
|