@data-slot/toggle 0.2.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +197 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +29 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +1 -0
- package/package.json +43 -0
package/README.md
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
# @data-slot/toggle
|
|
2
|
+
|
|
3
|
+
Headless toggle button component for vanilla JavaScript. Accessible, unstyled, tiny.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @data-slot/toggle
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
```html
|
|
14
|
+
<button data-slot="toggle">Bold</button>
|
|
15
|
+
<button data-slot="toggle" data-default-pressed>Italic</button>
|
|
16
|
+
|
|
17
|
+
<script type="module">
|
|
18
|
+
import { create } from "@data-slot/toggle";
|
|
19
|
+
|
|
20
|
+
const controllers = create();
|
|
21
|
+
</script>
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## API
|
|
25
|
+
|
|
26
|
+
### `create(scope?)`
|
|
27
|
+
|
|
28
|
+
Auto-discover and bind all toggle instances in a scope (defaults to `document`).
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { create } from "@data-slot/toggle";
|
|
32
|
+
|
|
33
|
+
const controllers = create(); // Returns ToggleController[]
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### `createToggle(root, options?)`
|
|
37
|
+
|
|
38
|
+
Create a controller for a specific element.
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
import { createToggle } from "@data-slot/toggle";
|
|
42
|
+
|
|
43
|
+
const toggle = createToggle(element, {
|
|
44
|
+
defaultPressed: false,
|
|
45
|
+
disabled: false,
|
|
46
|
+
onPressedChange: (pressed) => console.log(pressed),
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Options
|
|
51
|
+
|
|
52
|
+
| Option | Type | Default | Description |
|
|
53
|
+
|--------|------|---------|-------------|
|
|
54
|
+
| `defaultPressed` | `boolean` | `false` | Initial pressed state |
|
|
55
|
+
| `disabled` | `boolean` | `false` | Disabled state |
|
|
56
|
+
| `onPressedChange` | `(pressed: boolean) => void` | `undefined` | Callback when state changes |
|
|
57
|
+
|
|
58
|
+
### Data Attributes
|
|
59
|
+
|
|
60
|
+
Options can also be set via data attributes on the root element. JS options take precedence.
|
|
61
|
+
|
|
62
|
+
| Attribute | Type | Default | Description |
|
|
63
|
+
|-----------|------|---------|-------------|
|
|
64
|
+
| `data-default-pressed` | boolean | `false` | Initial pressed state |
|
|
65
|
+
| `data-disabled` | boolean | `false` | Disabled state |
|
|
66
|
+
|
|
67
|
+
```html
|
|
68
|
+
<!-- Initially pressed toggle -->
|
|
69
|
+
<button data-slot="toggle" data-default-pressed>Bold</button>
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Controller
|
|
73
|
+
|
|
74
|
+
| Method/Property | Description |
|
|
75
|
+
|-----------------|-------------|
|
|
76
|
+
| `toggle()` | Toggle the pressed state (ignores disabled) |
|
|
77
|
+
| `press()` | Set pressed to true (ignores disabled) |
|
|
78
|
+
| `release()` | Set pressed to false (ignores disabled) |
|
|
79
|
+
| `pressed` | Current pressed state (readonly `boolean`) |
|
|
80
|
+
| `destroy()` | Cleanup all event listeners |
|
|
81
|
+
|
|
82
|
+
**Note:** Controller methods always work, even when disabled. This allows programmatic control regardless of user interaction state. If you need to check disabled state before calling controller methods, check the element's attributes yourself.
|
|
83
|
+
|
|
84
|
+
## Markup Structure
|
|
85
|
+
|
|
86
|
+
```html
|
|
87
|
+
<button data-slot="toggle">Label</button>
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
The toggle is a simple single-element component. Always use a native `<button>` element—keyboard support (Enter/Space) and focus handling are only guaranteed with `<button>`. Non-button elements (e.g., `<div>`, `<span>`) are not recommended and would require manual keyboard handling.
|
|
91
|
+
|
|
92
|
+
## Styling
|
|
93
|
+
|
|
94
|
+
### State Attributes
|
|
95
|
+
|
|
96
|
+
The component sets these attributes for styling:
|
|
97
|
+
|
|
98
|
+
- `aria-pressed="true|false"` - ARIA state
|
|
99
|
+
- `data-state="on|off"` - CSS styling hook
|
|
100
|
+
|
|
101
|
+
### Basic Styling
|
|
102
|
+
|
|
103
|
+
```css
|
|
104
|
+
/* Unpressed state */
|
|
105
|
+
[data-slot="toggle"] {
|
|
106
|
+
background: #e5e7eb;
|
|
107
|
+
border: none;
|
|
108
|
+
padding: 0.5rem 1rem;
|
|
109
|
+
cursor: pointer;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/* Pressed state */
|
|
113
|
+
[data-slot="toggle"][data-state="on"] {
|
|
114
|
+
background: #3b82f6;
|
|
115
|
+
color: white;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/* Or use aria-pressed */
|
|
119
|
+
[data-slot="toggle"][aria-pressed="true"] {
|
|
120
|
+
background: #3b82f6;
|
|
121
|
+
color: white;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/* Disabled state */
|
|
125
|
+
[data-slot="toggle"][aria-disabled="true"] {
|
|
126
|
+
opacity: 0.5;
|
|
127
|
+
cursor: not-allowed;
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Tailwind Example
|
|
132
|
+
|
|
133
|
+
```html
|
|
134
|
+
<button
|
|
135
|
+
data-slot="toggle"
|
|
136
|
+
class="px-4 py-2 rounded bg-gray-200 data-[state=on]:bg-blue-500 data-[state=on]:text-white aria-disabled:opacity-50"
|
|
137
|
+
>
|
|
138
|
+
Bold
|
|
139
|
+
</button>
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Keyboard Support
|
|
143
|
+
|
|
144
|
+
The toggle uses a native `<button>` element, so keyboard support is automatic:
|
|
145
|
+
|
|
146
|
+
| Key | Action |
|
|
147
|
+
|-----|--------|
|
|
148
|
+
| `Enter` | Toggle state |
|
|
149
|
+
| `Space` | Toggle state |
|
|
150
|
+
|
|
151
|
+
## Disabled Behavior
|
|
152
|
+
|
|
153
|
+
When disabled (via `disabled` option, `data-disabled` attribute, native `disabled` attribute, or `aria-disabled="true"`):
|
|
154
|
+
|
|
155
|
+
| Input | Blocked? |
|
|
156
|
+
|-------|----------|
|
|
157
|
+
| Click / Enter / Space | Yes |
|
|
158
|
+
| `toggle:set` event | Yes |
|
|
159
|
+
| Controller methods (`toggle()`, `press()`, `release()`) | No |
|
|
160
|
+
|
|
161
|
+
For `<button>` elements, the `disabled` option sets both the native `disabled` attribute and `aria-disabled="true"`. For other elements, only `aria-disabled` is set.
|
|
162
|
+
|
|
163
|
+
## Accessibility
|
|
164
|
+
|
|
165
|
+
The component automatically handles:
|
|
166
|
+
|
|
167
|
+
- `aria-pressed` state on the button
|
|
168
|
+
- Native `disabled` and `aria-disabled` when disabled (for buttons)
|
|
169
|
+
- `type="button"` to prevent form submission
|
|
170
|
+
|
|
171
|
+
## Events
|
|
172
|
+
|
|
173
|
+
Listen for changes via custom events:
|
|
174
|
+
|
|
175
|
+
```javascript
|
|
176
|
+
element.addEventListener("toggle:change", (e) => {
|
|
177
|
+
console.log("Pressed:", e.detail.pressed);
|
|
178
|
+
});
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Control the toggle via events (ignored when disabled):
|
|
182
|
+
|
|
183
|
+
```javascript
|
|
184
|
+
// Set to specific state
|
|
185
|
+
element.dispatchEvent(
|
|
186
|
+
new CustomEvent("toggle:set", { detail: { pressed: true } })
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
// Or with just a boolean
|
|
190
|
+
element.dispatchEvent(
|
|
191
|
+
new CustomEvent("toggle:set", { detail: true })
|
|
192
|
+
);
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
## License
|
|
196
|
+
|
|
197
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e=(e,t)=>[...e.querySelectorAll(`[data-slot="${t}"]`)],t=new WeakMap;function n(e,n,r){if(typeof process<`u`&&process.env?.NODE_ENV===`production`)return;let i=t.get(e);i||(i=new Set,t.set(e,i)),!i.has(n)&&(i.add(n),console.warn(`[@data-slot] ${r}`))}function r(e){let t=`data-${e.replace(/([A-Z])/g,`-$1`).toLowerCase()}`,n=`data-${e}`;return t===n?[t]:[t,n]}function i(e,t){for(let n of r(t))if(e.hasAttribute(n))return e.getAttribute(n);return null}function a(e,t){return r(t).some(t=>e.hasAttribute(t))}const o=new Set([``,`true`,`1`,`yes`]),s=new Set([`false`,`0`,`no`]);function c(e,t){if(!a(e,t))return;let r=i(e,t);if(r===null)return;let c=r.toLowerCase();if(o.has(c))return!0;if(s.has(c))return!1;n(e,t,`Invalid boolean value "${r}" for data-${t}. Expected: true/false/1/0/yes/no or empty.`)}const l=(e,t,n)=>{n===null?e.removeAttribute(`aria-${t}`):e.setAttribute(`aria-${t}`,String(n))};function u(e,t,n,r){return e.addEventListener(t,n,r),()=>e.removeEventListener(t,n,r)}const d=(e,t,n)=>e.dispatchEvent(new CustomEvent(t,{bubbles:!0,detail:n}));function f(e,t={}){let n=t.defaultPressed??c(e,`defaultPressed`)??!1,r=t.disabled??c(e,`disabled`)??!1,i=t.onPressedChange,a=n,o=[],s=()=>e.hasAttribute(`disabled`)||e.getAttribute(`aria-disabled`)===`true`,f=(t,n=!1)=>{a===t&&!n||(a=t,l(e,`pressed`,a),e.dataset.state=a?`on`:`off`,n||(d(e,`toggle:change`,{pressed:a}),i?.(a)))};return r&&(e.tagName===`BUTTON`&&e.setAttribute(`disabled`,``),e.setAttribute(`aria-disabled`,`true`)),e.tagName===`BUTTON`&&!e.hasAttribute(`type`)&&e.setAttribute(`type`,`button`),f(a,!0),o.push(u(e,`click`,()=>{s()||f(!a)})),o.push(u(e,`toggle:set`,e=>{if(s())return;let t=e.detail,n=typeof t==`boolean`?t:t?.pressed;typeof n==`boolean`&&f(n)})),{toggle:()=>f(!a),press:()=>f(!0),release:()=>f(!1),get pressed(){return a},destroy:()=>{o.forEach(e=>e()),o.length=0,p.delete(e)}}}const p=new WeakSet;function m(t=document){let n=[];for(let r of e(t,`toggle`)){if(p.has(r))continue;p.add(r),n.push(f(r))}return n}exports.create=m,exports.createToggle=f;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
//#region src/index.d.ts
|
|
2
|
+
interface ToggleOptions {
|
|
3
|
+
/** Initial pressed state */
|
|
4
|
+
defaultPressed?: boolean;
|
|
5
|
+
/** Disabled state */
|
|
6
|
+
disabled?: boolean;
|
|
7
|
+
/** Callback when pressed state changes */
|
|
8
|
+
onPressedChange?: (pressed: boolean) => void;
|
|
9
|
+
}
|
|
10
|
+
interface ToggleController {
|
|
11
|
+
/** Toggle the pressed state (always works, ignores disabled) */
|
|
12
|
+
toggle(): void;
|
|
13
|
+
/** Set pressed to true (always works, ignores disabled) */
|
|
14
|
+
press(): void;
|
|
15
|
+
/** Set pressed to false (always works, ignores disabled) */
|
|
16
|
+
release(): void;
|
|
17
|
+
/** Current pressed state */
|
|
18
|
+
readonly pressed: boolean;
|
|
19
|
+
/** Cleanup all event listeners */
|
|
20
|
+
destroy(): void;
|
|
21
|
+
}
|
|
22
|
+
declare function createToggle(root: HTMLElement, options?: ToggleOptions): ToggleController;
|
|
23
|
+
/**
|
|
24
|
+
* Find and bind all toggle instances in a scope
|
|
25
|
+
* Returns array of controllers for programmatic access
|
|
26
|
+
*/
|
|
27
|
+
declare function create(scope?: ParentNode): ToggleController[];
|
|
28
|
+
//#endregion
|
|
29
|
+
export { ToggleController, ToggleOptions, create, createToggle };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
//#region src/index.d.ts
|
|
2
|
+
interface ToggleOptions {
|
|
3
|
+
/** Initial pressed state */
|
|
4
|
+
defaultPressed?: boolean;
|
|
5
|
+
/** Disabled state */
|
|
6
|
+
disabled?: boolean;
|
|
7
|
+
/** Callback when pressed state changes */
|
|
8
|
+
onPressedChange?: (pressed: boolean) => void;
|
|
9
|
+
}
|
|
10
|
+
interface ToggleController {
|
|
11
|
+
/** Toggle the pressed state (always works, ignores disabled) */
|
|
12
|
+
toggle(): void;
|
|
13
|
+
/** Set pressed to true (always works, ignores disabled) */
|
|
14
|
+
press(): void;
|
|
15
|
+
/** Set pressed to false (always works, ignores disabled) */
|
|
16
|
+
release(): void;
|
|
17
|
+
/** Current pressed state */
|
|
18
|
+
readonly pressed: boolean;
|
|
19
|
+
/** Cleanup all event listeners */
|
|
20
|
+
destroy(): void;
|
|
21
|
+
}
|
|
22
|
+
declare function createToggle(root: HTMLElement, options?: ToggleOptions): ToggleController;
|
|
23
|
+
/**
|
|
24
|
+
* Find and bind all toggle instances in a scope
|
|
25
|
+
* Returns array of controllers for programmatic access
|
|
26
|
+
*/
|
|
27
|
+
declare function create(scope?: ParentNode): ToggleController[];
|
|
28
|
+
//#endregion
|
|
29
|
+
export { ToggleController, ToggleOptions, create, createToggle };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
const e=(e,t)=>[...e.querySelectorAll(`[data-slot="${t}"]`)],t=new WeakMap;function n(e,n,r){if(typeof process<`u`&&process.env?.NODE_ENV===`production`)return;let i=t.get(e);i||(i=new Set,t.set(e,i)),!i.has(n)&&(i.add(n),console.warn(`[@data-slot] ${r}`))}function r(e){let t=`data-${e.replace(/([A-Z])/g,`-$1`).toLowerCase()}`,n=`data-${e}`;return t===n?[t]:[t,n]}function i(e,t){for(let n of r(t))if(e.hasAttribute(n))return e.getAttribute(n);return null}function a(e,t){return r(t).some(t=>e.hasAttribute(t))}const o=new Set([``,`true`,`1`,`yes`]),s=new Set([`false`,`0`,`no`]);function c(e,t){if(!a(e,t))return;let r=i(e,t);if(r===null)return;let c=r.toLowerCase();if(o.has(c))return!0;if(s.has(c))return!1;n(e,t,`Invalid boolean value "${r}" for data-${t}. Expected: true/false/1/0/yes/no or empty.`)}const l=(e,t,n)=>{n===null?e.removeAttribute(`aria-${t}`):e.setAttribute(`aria-${t}`,String(n))};function u(e,t,n,r){return e.addEventListener(t,n,r),()=>e.removeEventListener(t,n,r)}const d=(e,t,n)=>e.dispatchEvent(new CustomEvent(t,{bubbles:!0,detail:n}));function f(e,t={}){let n=t.defaultPressed??c(e,`defaultPressed`)??!1,r=t.disabled??c(e,`disabled`)??!1,i=t.onPressedChange,a=n,o=[],s=()=>e.hasAttribute(`disabled`)||e.getAttribute(`aria-disabled`)===`true`,f=(t,n=!1)=>{a===t&&!n||(a=t,l(e,`pressed`,a),e.dataset.state=a?`on`:`off`,n||(d(e,`toggle:change`,{pressed:a}),i?.(a)))};return r&&(e.tagName===`BUTTON`&&e.setAttribute(`disabled`,``),e.setAttribute(`aria-disabled`,`true`)),e.tagName===`BUTTON`&&!e.hasAttribute(`type`)&&e.setAttribute(`type`,`button`),f(a,!0),o.push(u(e,`click`,()=>{s()||f(!a)})),o.push(u(e,`toggle:set`,e=>{if(s())return;let t=e.detail,n=typeof t==`boolean`?t:t?.pressed;typeof n==`boolean`&&f(n)})),{toggle:()=>f(!a),press:()=>f(!0),release:()=>f(!1),get pressed(){return a},destroy:()=>{o.forEach(e=>e()),o.length=0,p.delete(e)}}}const p=new WeakSet;function m(t=document){let n=[];for(let r of e(t,`toggle`)){if(p.has(r))continue;p.add(r),n.push(f(r))}return n}export{m as create,f as createToggle};
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@data-slot/toggle",
|
|
3
|
+
"version": "0.2.10",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsdown"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@data-slot/core": "workspace:*"
|
|
29
|
+
},
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "https://github.com/bejamas/data-slot",
|
|
33
|
+
"directory": "packages/toggle"
|
|
34
|
+
},
|
|
35
|
+
"keywords": [
|
|
36
|
+
"headless",
|
|
37
|
+
"ui",
|
|
38
|
+
"toggle",
|
|
39
|
+
"vanilla",
|
|
40
|
+
"data-slot"
|
|
41
|
+
],
|
|
42
|
+
"license": "MIT"
|
|
43
|
+
}
|