@nucleux/md3-segmented-button 0.4.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 +26 -0
- package/dist/index.cjs +92 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +18 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +69 -0
- package/dist/index.js.map +1 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nucleux
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# @nucleux/md3-segmented-button
|
|
2
|
+
|
|
3
|
+
Material 3 segmented button — Nucleux agentic UI for React.
|
|
4
|
+
|
|
5
|
+
Part of **[Nucleux](https://github.com/intelligaia-open-labs/nucleux)** — agentic UI components for React. Install a single component, or the full kit via [`@nucleux/react`](https://www.npmjs.com/package/@nucleux/react).
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add @nucleux/md3-segmented-button @nucleux/tokens
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
`react` / `react-dom` are peer dependencies.
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```tsx
|
|
18
|
+
import { Md3SegmentedButton } from "@nucleux/md3-segmented-button";
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Requires the theme once in your app: `import "@nucleux/tokens/styles.css";`
|
|
22
|
+
and the Tailwind preset from `@nucleux/tokens/preset`.
|
|
23
|
+
|
|
24
|
+
## License
|
|
25
|
+
|
|
26
|
+
MIT © Nucleux
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var React = require('react');
|
|
5
|
+
var lucideReact = require('lucide-react');
|
|
6
|
+
var utils = require('@nucleux/utils');
|
|
7
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
8
|
+
|
|
9
|
+
function _interopNamespace(e) {
|
|
10
|
+
if (e && e.__esModule) return e;
|
|
11
|
+
var n = Object.create(null);
|
|
12
|
+
if (e) {
|
|
13
|
+
Object.keys(e).forEach(function (k) {
|
|
14
|
+
if (k !== 'default') {
|
|
15
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
16
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
get: function () { return e[k]; }
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
n.default = e;
|
|
24
|
+
return Object.freeze(n);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
var React__namespace = /*#__PURE__*/_interopNamespace(React);
|
|
28
|
+
|
|
29
|
+
// src/index.tsx
|
|
30
|
+
var SegmentedContext = React__namespace.createContext(null);
|
|
31
|
+
var asArray = (v) => v === void 0 ? [] : Array.isArray(v) ? v : [v];
|
|
32
|
+
var Md3SegmentedButton = React__namespace.forwardRef(
|
|
33
|
+
({ type = "single", value, defaultValue, onValueChange, className, children, ...props }, ref) => {
|
|
34
|
+
const multiple = type === "multiple";
|
|
35
|
+
const isControlled = value !== void 0;
|
|
36
|
+
const [internal, setInternal] = React__namespace.useState(asArray(defaultValue));
|
|
37
|
+
const current = isControlled ? asArray(value) : internal;
|
|
38
|
+
const toggle = (v) => {
|
|
39
|
+
let next;
|
|
40
|
+
if (multiple) next = current.includes(v) ? current.filter((x) => x !== v) : [...current, v];
|
|
41
|
+
else next = current[0] === v ? [] : [v];
|
|
42
|
+
if (!isControlled) setInternal(next);
|
|
43
|
+
onValueChange?.(multiple ? next : next[0] ?? "");
|
|
44
|
+
};
|
|
45
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
46
|
+
"div",
|
|
47
|
+
{
|
|
48
|
+
ref,
|
|
49
|
+
role: "group",
|
|
50
|
+
className: utils.cn("inline-flex h-10 divide-x divide-md-outline overflow-hidden rounded-full border border-md-outline", className),
|
|
51
|
+
...props,
|
|
52
|
+
children: /* @__PURE__ */ jsxRuntime.jsx(SegmentedContext.Provider, { value: { value: current, toggle, multiple }, children })
|
|
53
|
+
}
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
);
|
|
57
|
+
Md3SegmentedButton.displayName = "Md3SegmentedButton";
|
|
58
|
+
var Md3SegmentedButtonItem = React__namespace.forwardRef(
|
|
59
|
+
({ value, icon, className, children, ...props }, ref) => {
|
|
60
|
+
const ctx = React__namespace.useContext(SegmentedContext);
|
|
61
|
+
if (!ctx) throw new Error("Md3SegmentedButtonItem must be used within a Md3SegmentedButton");
|
|
62
|
+
const selected = ctx.value.includes(value);
|
|
63
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(
|
|
64
|
+
"button",
|
|
65
|
+
{
|
|
66
|
+
ref,
|
|
67
|
+
type: "button",
|
|
68
|
+
"aria-pressed": selected,
|
|
69
|
+
onClick: () => ctx.toggle(value),
|
|
70
|
+
className: utils.cn(
|
|
71
|
+
"relative inline-flex items-center justify-center gap-2 overflow-hidden px-4 text-sm font-medium transition-colors [&_svg]:size-[18px]",
|
|
72
|
+
"before:absolute before:inset-0 before:bg-current before:opacity-0 before:transition-opacity before:content-['']",
|
|
73
|
+
"hover:before:opacity-[0.08] focus-visible:before:opacity-[0.12]",
|
|
74
|
+
"outline-none focus-visible:z-10 focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-md-primary",
|
|
75
|
+
selected ? "bg-md-secondary-container text-md-on-secondary-container" : "text-md-on-surface",
|
|
76
|
+
className
|
|
77
|
+
),
|
|
78
|
+
...props,
|
|
79
|
+
children: [
|
|
80
|
+
selected ? /* @__PURE__ */ jsxRuntime.jsx(lucideReact.Check, { "aria-hidden": true, className: "relative shrink-0" }) : icon && /* @__PURE__ */ jsxRuntime.jsx("span", { className: "relative shrink-0", children: icon }),
|
|
81
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { className: "relative", children })
|
|
82
|
+
]
|
|
83
|
+
}
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
);
|
|
87
|
+
Md3SegmentedButtonItem.displayName = "Md3SegmentedButtonItem";
|
|
88
|
+
|
|
89
|
+
exports.Md3SegmentedButton = Md3SegmentedButton;
|
|
90
|
+
exports.Md3SegmentedButtonItem = Md3SegmentedButtonItem;
|
|
91
|
+
//# sourceMappingURL=index.cjs.map
|
|
92
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts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A,EAAA,EAAM,aAAA,EAAW,IAAA,EAAC,SAAA,EAAU,mBAAA,EAAoB,CAAA,GAEjD,IAAA,oBAAQH,cAAA,CAAC,MAAA,EAAA,EAAK,SAAA,EAAU,mBAAA,EAAqB,QAAA,EAAA,IAAA,EAAK,CAAA;AAAA,0BAEpDA,cAAA,CAAC,MAAA,EAAA,EAAK,SAAA,EAAU,UAAA,EAAY,QAAA,EAAS;AAAA;AAAA;AAAA,KACvC;AAAA,EAEJ;AACF;AACA,sBAAA,CAAuB,WAAA,GAAc,wBAAA","file":"index.cjs","sourcesContent":["import * as React from \"react\";\nimport { Check } from \"lucide-react\";\nimport { cn } from \"@nucleux/utils\";\n\ninterface SegmentedContextValue {\n value: string[];\n toggle: (v: string) => void;\n multiple: boolean;\n}\nconst SegmentedContext = React.createContext<SegmentedContextValue | null>(null);\n\nexport interface Md3SegmentedButtonProps\n extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onChange\" | \"defaultValue\"> {\n type?: \"single\" | \"multiple\";\n value?: string | string[];\n defaultValue?: string | string[];\n onValueChange?: (value: string | string[]) => void;\n}\n\nconst asArray = (v: string | string[] | undefined) => (v === undefined ? [] : Array.isArray(v) ? v : [v]);\n\n/** A Material Design 3 segmented button group. Compose with Md3SegmentedButtonItem. */\nexport const Md3SegmentedButton = React.forwardRef<HTMLDivElement, Md3SegmentedButtonProps>(\n ({ type = \"single\", value, defaultValue, onValueChange, className, children, ...props }, ref) => {\n const multiple = type === \"multiple\";\n const isControlled = value !== undefined;\n const [internal, setInternal] = React.useState<string[]>(asArray(defaultValue));\n const current = isControlled ? asArray(value) : internal;\n\n const toggle = (v: string) => {\n let next: string[];\n if (multiple) next = current.includes(v) ? current.filter((x) => x !== v) : [...current, v];\n else next = current[0] === v ? [] : [v];\n if (!isControlled) setInternal(next);\n onValueChange?.(multiple ? next : (next[0] ?? \"\"));\n };\n\n return (\n <div\n ref={ref}\n role=\"group\"\n className={cn(\"inline-flex h-10 divide-x divide-md-outline overflow-hidden rounded-full border border-md-outline\", className)}\n {...props}\n >\n <SegmentedContext.Provider value={{ value: current, toggle, multiple }}>\n {children}\n </SegmentedContext.Provider>\n </div>\n );\n },\n);\nMd3SegmentedButton.displayName = \"Md3SegmentedButton\";\n\nexport interface Md3SegmentedButtonItemProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {\n value: string;\n icon?: React.ReactNode;\n}\n\n/** A single segment within a {@link Md3SegmentedButton}. */\nexport const Md3SegmentedButtonItem = React.forwardRef<HTMLButtonElement, Md3SegmentedButtonItemProps>(\n ({ value, icon, className, children, ...props }, ref) => {\n const ctx = React.useContext(SegmentedContext);\n if (!ctx) throw new Error(\"Md3SegmentedButtonItem must be used within a Md3SegmentedButton\");\n const selected = ctx.value.includes(value);\n\n return (\n <button\n ref={ref}\n type=\"button\"\n aria-pressed={selected}\n onClick={() => ctx.toggle(value)}\n className={cn(\n \"relative inline-flex items-center justify-center gap-2 overflow-hidden px-4 text-sm font-medium transition-colors [&_svg]:size-[18px]\",\n \"before:absolute before:inset-0 before:bg-current before:opacity-0 before:transition-opacity before:content-['']\",\n \"hover:before:opacity-[0.08] focus-visible:before:opacity-[0.12]\",\n \"outline-none focus-visible:z-10 focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-md-primary\",\n selected ? \"bg-md-secondary-container text-md-on-secondary-container\" : \"text-md-on-surface\",\n className,\n )}\n {...props}\n >\n {selected ? (\n <Check aria-hidden className=\"relative shrink-0\" />\n ) : (\n icon && <span className=\"relative shrink-0\">{icon}</span>\n )}\n <span className=\"relative\">{children}</span>\n </button>\n );\n },\n);\nMd3SegmentedButtonItem.displayName = \"Md3SegmentedButtonItem\";\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
interface Md3SegmentedButtonProps extends Omit<React.HTMLAttributes<HTMLDivElement>, "onChange" | "defaultValue"> {
|
|
4
|
+
type?: "single" | "multiple";
|
|
5
|
+
value?: string | string[];
|
|
6
|
+
defaultValue?: string | string[];
|
|
7
|
+
onValueChange?: (value: string | string[]) => void;
|
|
8
|
+
}
|
|
9
|
+
/** A Material Design 3 segmented button group. Compose with Md3SegmentedButtonItem. */
|
|
10
|
+
declare const Md3SegmentedButton: React.ForwardRefExoticComponent<Md3SegmentedButtonProps & React.RefAttributes<HTMLDivElement>>;
|
|
11
|
+
interface Md3SegmentedButtonItemProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
|
|
12
|
+
value: string;
|
|
13
|
+
icon?: React.ReactNode;
|
|
14
|
+
}
|
|
15
|
+
/** A single segment within a {@link Md3SegmentedButton}. */
|
|
16
|
+
declare const Md3SegmentedButtonItem: React.ForwardRefExoticComponent<Md3SegmentedButtonItemProps & React.RefAttributes<HTMLButtonElement>>;
|
|
17
|
+
|
|
18
|
+
export { Md3SegmentedButton, Md3SegmentedButtonItem, type Md3SegmentedButtonItemProps, type Md3SegmentedButtonProps };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
interface Md3SegmentedButtonProps extends Omit<React.HTMLAttributes<HTMLDivElement>, "onChange" | "defaultValue"> {
|
|
4
|
+
type?: "single" | "multiple";
|
|
5
|
+
value?: string | string[];
|
|
6
|
+
defaultValue?: string | string[];
|
|
7
|
+
onValueChange?: (value: string | string[]) => void;
|
|
8
|
+
}
|
|
9
|
+
/** A Material Design 3 segmented button group. Compose with Md3SegmentedButtonItem. */
|
|
10
|
+
declare const Md3SegmentedButton: React.ForwardRefExoticComponent<Md3SegmentedButtonProps & React.RefAttributes<HTMLDivElement>>;
|
|
11
|
+
interface Md3SegmentedButtonItemProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
|
|
12
|
+
value: string;
|
|
13
|
+
icon?: React.ReactNode;
|
|
14
|
+
}
|
|
15
|
+
/** A single segment within a {@link Md3SegmentedButton}. */
|
|
16
|
+
declare const Md3SegmentedButtonItem: React.ForwardRefExoticComponent<Md3SegmentedButtonItemProps & React.RefAttributes<HTMLButtonElement>>;
|
|
17
|
+
|
|
18
|
+
export { Md3SegmentedButton, Md3SegmentedButtonItem, type Md3SegmentedButtonItemProps, type Md3SegmentedButtonProps };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { Check } from 'lucide-react';
|
|
4
|
+
import { cn } from '@nucleux/utils';
|
|
5
|
+
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
6
|
+
|
|
7
|
+
// src/index.tsx
|
|
8
|
+
var SegmentedContext = React.createContext(null);
|
|
9
|
+
var asArray = (v) => v === void 0 ? [] : Array.isArray(v) ? v : [v];
|
|
10
|
+
var Md3SegmentedButton = React.forwardRef(
|
|
11
|
+
({ type = "single", value, defaultValue, onValueChange, className, children, ...props }, ref) => {
|
|
12
|
+
const multiple = type === "multiple";
|
|
13
|
+
const isControlled = value !== void 0;
|
|
14
|
+
const [internal, setInternal] = React.useState(asArray(defaultValue));
|
|
15
|
+
const current = isControlled ? asArray(value) : internal;
|
|
16
|
+
const toggle = (v) => {
|
|
17
|
+
let next;
|
|
18
|
+
if (multiple) next = current.includes(v) ? current.filter((x) => x !== v) : [...current, v];
|
|
19
|
+
else next = current[0] === v ? [] : [v];
|
|
20
|
+
if (!isControlled) setInternal(next);
|
|
21
|
+
onValueChange?.(multiple ? next : next[0] ?? "");
|
|
22
|
+
};
|
|
23
|
+
return /* @__PURE__ */ jsx(
|
|
24
|
+
"div",
|
|
25
|
+
{
|
|
26
|
+
ref,
|
|
27
|
+
role: "group",
|
|
28
|
+
className: cn("inline-flex h-10 divide-x divide-md-outline overflow-hidden rounded-full border border-md-outline", className),
|
|
29
|
+
...props,
|
|
30
|
+
children: /* @__PURE__ */ jsx(SegmentedContext.Provider, { value: { value: current, toggle, multiple }, children })
|
|
31
|
+
}
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
);
|
|
35
|
+
Md3SegmentedButton.displayName = "Md3SegmentedButton";
|
|
36
|
+
var Md3SegmentedButtonItem = React.forwardRef(
|
|
37
|
+
({ value, icon, className, children, ...props }, ref) => {
|
|
38
|
+
const ctx = React.useContext(SegmentedContext);
|
|
39
|
+
if (!ctx) throw new Error("Md3SegmentedButtonItem must be used within a Md3SegmentedButton");
|
|
40
|
+
const selected = ctx.value.includes(value);
|
|
41
|
+
return /* @__PURE__ */ jsxs(
|
|
42
|
+
"button",
|
|
43
|
+
{
|
|
44
|
+
ref,
|
|
45
|
+
type: "button",
|
|
46
|
+
"aria-pressed": selected,
|
|
47
|
+
onClick: () => ctx.toggle(value),
|
|
48
|
+
className: cn(
|
|
49
|
+
"relative inline-flex items-center justify-center gap-2 overflow-hidden px-4 text-sm font-medium transition-colors [&_svg]:size-[18px]",
|
|
50
|
+
"before:absolute before:inset-0 before:bg-current before:opacity-0 before:transition-opacity before:content-['']",
|
|
51
|
+
"hover:before:opacity-[0.08] focus-visible:before:opacity-[0.12]",
|
|
52
|
+
"outline-none focus-visible:z-10 focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-md-primary",
|
|
53
|
+
selected ? "bg-md-secondary-container text-md-on-secondary-container" : "text-md-on-surface",
|
|
54
|
+
className
|
|
55
|
+
),
|
|
56
|
+
...props,
|
|
57
|
+
children: [
|
|
58
|
+
selected ? /* @__PURE__ */ jsx(Check, { "aria-hidden": true, className: "relative shrink-0" }) : icon && /* @__PURE__ */ jsx("span", { className: "relative shrink-0", children: icon }),
|
|
59
|
+
/* @__PURE__ */ jsx("span", { className: "relative", children })
|
|
60
|
+
]
|
|
61
|
+
}
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
);
|
|
65
|
+
Md3SegmentedButtonItem.displayName = "Md3SegmentedButtonItem";
|
|
66
|
+
|
|
67
|
+
export { Md3SegmentedButton, Md3SegmentedButtonItem };
|
|
68
|
+
//# sourceMappingURL=index.js.map
|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A,mBACC,GAAA,CAAC,KAAA,EAAA,EAAM,aAAA,EAAW,IAAA,EAAC,SAAA,EAAU,mBAAA,EAAoB,CAAA,GAEjD,IAAA,oBAAQ,GAAA,CAAC,MAAA,EAAA,EAAK,SAAA,EAAU,mBAAA,EAAqB,QAAA,EAAA,IAAA,EAAK,CAAA;AAAA,0BAEpD,GAAA,CAAC,MAAA,EAAA,EAAK,SAAA,EAAU,UAAA,EAAY,QAAA,EAAS;AAAA;AAAA;AAAA,KACvC;AAAA,EAEJ;AACF;AACA,sBAAA,CAAuB,WAAA,GAAc,wBAAA","file":"index.js","sourcesContent":["import * as React from \"react\";\nimport { Check } from \"lucide-react\";\nimport { cn } from \"@nucleux/utils\";\n\ninterface SegmentedContextValue {\n value: string[];\n toggle: (v: string) => void;\n multiple: boolean;\n}\nconst SegmentedContext = React.createContext<SegmentedContextValue | null>(null);\n\nexport interface Md3SegmentedButtonProps\n extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onChange\" | \"defaultValue\"> {\n type?: \"single\" | \"multiple\";\n value?: string | string[];\n defaultValue?: string | string[];\n onValueChange?: (value: string | string[]) => void;\n}\n\nconst asArray = (v: string | string[] | undefined) => (v === undefined ? [] : Array.isArray(v) ? v : [v]);\n\n/** A Material Design 3 segmented button group. Compose with Md3SegmentedButtonItem. */\nexport const Md3SegmentedButton = React.forwardRef<HTMLDivElement, Md3SegmentedButtonProps>(\n ({ type = \"single\", value, defaultValue, onValueChange, className, children, ...props }, ref) => {\n const multiple = type === \"multiple\";\n const isControlled = value !== undefined;\n const [internal, setInternal] = React.useState<string[]>(asArray(defaultValue));\n const current = isControlled ? asArray(value) : internal;\n\n const toggle = (v: string) => {\n let next: string[];\n if (multiple) next = current.includes(v) ? current.filter((x) => x !== v) : [...current, v];\n else next = current[0] === v ? [] : [v];\n if (!isControlled) setInternal(next);\n onValueChange?.(multiple ? next : (next[0] ?? \"\"));\n };\n\n return (\n <div\n ref={ref}\n role=\"group\"\n className={cn(\"inline-flex h-10 divide-x divide-md-outline overflow-hidden rounded-full border border-md-outline\", className)}\n {...props}\n >\n <SegmentedContext.Provider value={{ value: current, toggle, multiple }}>\n {children}\n </SegmentedContext.Provider>\n </div>\n );\n },\n);\nMd3SegmentedButton.displayName = \"Md3SegmentedButton\";\n\nexport interface Md3SegmentedButtonItemProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {\n value: string;\n icon?: React.ReactNode;\n}\n\n/** A single segment within a {@link Md3SegmentedButton}. */\nexport const Md3SegmentedButtonItem = React.forwardRef<HTMLButtonElement, Md3SegmentedButtonItemProps>(\n ({ value, icon, className, children, ...props }, ref) => {\n const ctx = React.useContext(SegmentedContext);\n if (!ctx) throw new Error(\"Md3SegmentedButtonItem must be used within a Md3SegmentedButton\");\n const selected = ctx.value.includes(value);\n\n return (\n <button\n ref={ref}\n type=\"button\"\n aria-pressed={selected}\n onClick={() => ctx.toggle(value)}\n className={cn(\n \"relative inline-flex items-center justify-center gap-2 overflow-hidden px-4 text-sm font-medium transition-colors [&_svg]:size-[18px]\",\n \"before:absolute before:inset-0 before:bg-current before:opacity-0 before:transition-opacity before:content-['']\",\n \"hover:before:opacity-[0.08] focus-visible:before:opacity-[0.12]\",\n \"outline-none focus-visible:z-10 focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-md-primary\",\n selected ? \"bg-md-secondary-container text-md-on-secondary-container\" : \"text-md-on-surface\",\n className,\n )}\n {...props}\n >\n {selected ? (\n <Check aria-hidden className=\"relative shrink-0\" />\n ) : (\n icon && <span className=\"relative shrink-0\">{icon}</span>\n )}\n <span className=\"relative\">{children}</span>\n </button>\n );\n },\n);\nMd3SegmentedButtonItem.displayName = \"Md3SegmentedButtonItem\";\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nucleux/md3-segmented-button",
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"description": "Material 3 segmented button — Nucleux agentic UI for React.",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"nucleux",
|
|
9
|
+
"react",
|
|
10
|
+
"ui",
|
|
11
|
+
"components",
|
|
12
|
+
"agentic",
|
|
13
|
+
"ai",
|
|
14
|
+
"tailwind",
|
|
15
|
+
"md3-segmented-button"
|
|
16
|
+
],
|
|
17
|
+
"main": "./dist/index.cjs",
|
|
18
|
+
"module": "./dist/index.js",
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"import": "./dist/index.js",
|
|
24
|
+
"require": "./dist/index.cjs"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist",
|
|
29
|
+
"README.md"
|
|
30
|
+
],
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"react": ">=18",
|
|
33
|
+
"react-dom": ">=18"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"lucide-react": "^0.469.0",
|
|
37
|
+
"@nucleux/utils": "0.4.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"tsup": "^8.3.5",
|
|
41
|
+
"typescript": "^5.7.2",
|
|
42
|
+
"@types/react": "^18.3.18",
|
|
43
|
+
"react": "^18.3.1"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
},
|
|
48
|
+
"license": "MIT",
|
|
49
|
+
"repository": {
|
|
50
|
+
"type": "git",
|
|
51
|
+
"url": "git+https://github.com/intelligaia-open-labs/nucleux.git",
|
|
52
|
+
"directory": "packages/md3-segmented-button"
|
|
53
|
+
},
|
|
54
|
+
"homepage": "https://github.com/intelligaia-open-labs/nucleux#readme",
|
|
55
|
+
"scripts": {
|
|
56
|
+
"build": "tsup"
|
|
57
|
+
}
|
|
58
|
+
}
|