@archoleat/next-accordion 1.0.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 +118 -0
- package/index.d.ts +32 -0
- package/index.js +1 -0
- package/package.json +101 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Archoleat
|
|
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,118 @@
|
|
|
1
|
+
# Next Accordion
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+

|
|
7
|
+

|
|
8
|
+

|
|
9
|
+
|
|
10
|
+
## Table of Contents
|
|
11
|
+
|
|
12
|
+
- [Installation](#installation)
|
|
13
|
+
- [Usage](#usage)
|
|
14
|
+
- [Props](#props)
|
|
15
|
+
- [Contributing](#contributing)
|
|
16
|
+
- [License](#license)
|
|
17
|
+
|
|
18
|
+
Animated React accordion using details and summary
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
bun i -D @archoleat/next-accordion
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
### Single item
|
|
29
|
+
|
|
30
|
+
Render a single collapsible panel:
|
|
31
|
+
|
|
32
|
+
```tsx
|
|
33
|
+
import { AccordionItem } from '@archoleat/next-accordion';
|
|
34
|
+
|
|
35
|
+
const Example = () => (
|
|
36
|
+
<AccordionItem trigger="Trigger">
|
|
37
|
+
<p>Content</p>
|
|
38
|
+
</AccordionItem>
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
export { Example };
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### List of items
|
|
45
|
+
|
|
46
|
+
Render a list of panels from an array of data:
|
|
47
|
+
|
|
48
|
+
```tsx
|
|
49
|
+
import { Accordion } from '@archoleat/next-accordion';
|
|
50
|
+
|
|
51
|
+
const items = [
|
|
52
|
+
{ id: 1, trigger: 'First', content: <p>First content</p> },
|
|
53
|
+
{ id: 2, trigger: 'Second', content: <p>Second content</p> },
|
|
54
|
+
];
|
|
55
|
+
|
|
56
|
+
const Example = () => <Accordion exclusive items={items} />;
|
|
57
|
+
|
|
58
|
+
export { Example };
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Props
|
|
62
|
+
|
|
63
|
+
### `Accordion`
|
|
64
|
+
|
|
65
|
+
- `items` (`AccordionItemData[]`, required): data rendered as a list
|
|
66
|
+
of `AccordionItem`s. Each entry accepts `id` (`number`), `trigger`,
|
|
67
|
+
`content`, plus every `AccordionItem` prop below (except `children`,
|
|
68
|
+
which maps to `content`).
|
|
69
|
+
|
|
70
|
+
- `className` (`string`): class applied to the wrapping `div`.
|
|
71
|
+
|
|
72
|
+
- `exclusive` (`boolean`, default `false`): when `true`, opening one
|
|
73
|
+
item closes the others.
|
|
74
|
+
|
|
75
|
+
### `AccordionItem`
|
|
76
|
+
|
|
77
|
+
- `trigger` (`ReactNode`, required): content rendered inside
|
|
78
|
+
the `summary` element.
|
|
79
|
+
|
|
80
|
+
- `children` (`ReactNode`, required): content rendered inside
|
|
81
|
+
the accordion panel.
|
|
82
|
+
|
|
83
|
+
- `defaultOpen` (`boolean`, default `false`): initial expanded state
|
|
84
|
+
(uncontrolled).
|
|
85
|
+
|
|
86
|
+
- `open` (`boolean`): expanded state (controlled). Pair
|
|
87
|
+
with `onOpenChange`.
|
|
88
|
+
|
|
89
|
+
- `onOpenChange` (`(isOpen: boolean) => void`): called when the trigger
|
|
90
|
+
is clicked.
|
|
91
|
+
|
|
92
|
+
- `disabled` (`boolean`, default `false`): ignores clicks on the trigger.
|
|
93
|
+
|
|
94
|
+
- `duration` (`number`, default `300`): animation duration
|
|
95
|
+
in milliseconds.
|
|
96
|
+
|
|
97
|
+
- `easing` (`string`, default `'ease-out'`): animation easing, passed
|
|
98
|
+
to the Web Animations API.
|
|
99
|
+
|
|
100
|
+
- `icon` (`ReactNode` or `(isOpen: boolean) => ReactNode`): rendered
|
|
101
|
+
after `trigger`; the function form receives the open state.
|
|
102
|
+
|
|
103
|
+
- `disableTriggerSelection` (`boolean`, default `false`): prevents text
|
|
104
|
+
selection on the trigger (e.g. on double-click).
|
|
105
|
+
|
|
106
|
+
All other native `details` attributes (`id`, `className`, and so on) are
|
|
107
|
+
forwarded to the underlying `<details>` element. The animation is skipped
|
|
108
|
+
in favor of an instant toggle when the user has `prefers-reduced-motion`
|
|
109
|
+
enabled.
|
|
110
|
+
|
|
111
|
+
## Contributing
|
|
112
|
+
|
|
113
|
+
Please read [**CONTRIBUTING**](https://github.com/archoleat/.github/blob/main/CONTRIBUTING.md)
|
|
114
|
+
to start contributing.
|
|
115
|
+
|
|
116
|
+
## License
|
|
117
|
+
|
|
118
|
+
This project is licensed under the [**MIT license**](LICENSE).
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { DetailsHTMLAttributes, ReactNode } from 'react';
|
|
3
|
+
|
|
4
|
+
type AccordionItemProps = Omit<DetailsHTMLAttributes<HTMLDetailsElement>, 'children' | 'onToggle' | 'open'> & {
|
|
5
|
+
children: ReactNode;
|
|
6
|
+
defaultOpen?: boolean;
|
|
7
|
+
disabled?: boolean;
|
|
8
|
+
disableTriggerSelection?: boolean;
|
|
9
|
+
duration?: number;
|
|
10
|
+
easing?: string;
|
|
11
|
+
icon?: ReactNode | ((isOpen: boolean) => ReactNode);
|
|
12
|
+
onOpenChange?: (isOpen: boolean) => void;
|
|
13
|
+
open?: boolean;
|
|
14
|
+
trigger: ReactNode;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type AccordionItemData = Omit<AccordionItemProps, 'children' | 'content' | 'id'> & {
|
|
18
|
+
content: ReactNode;
|
|
19
|
+
id: number;
|
|
20
|
+
};
|
|
21
|
+
type AccordionProps = {
|
|
22
|
+
className?: string;
|
|
23
|
+
exclusive?: boolean;
|
|
24
|
+
items: AccordionItemData[];
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
declare const Accordion: (props: AccordionProps) => react.JSX.Element;
|
|
28
|
+
|
|
29
|
+
declare const AccordionItem: (props: AccordionItemProps) => react.JSX.Element;
|
|
30
|
+
|
|
31
|
+
export { Accordion, AccordionItem };
|
|
32
|
+
export type { AccordionItemData, AccordionItemProps, AccordionProps };
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{jsxs as F,jsx as x}from"react/jsx-runtime";import{useState as T,useRef as l,useLayoutEffect as P}from"react";const U=()=>typeof window<"u"&&typeof window.matchMedia=="function"&&window.matchMedia("(prefers-reduced-motion: reduce)").matches,$=300,D="ease-out",b=m=>{const{children:g,defaultOpen:f=!1,disabled:o=!1,disableTriggerSelection:u=!1,duration:i=$,easing:c=D,icon:r,onOpenChange:d,open:a,trigger:H,...I}=m,[p,L]=T(f),h=l(null),R=l(null),v=l(null),t=l(null),N=l(!0),O=a!==void 0,n=O?a:p,M=e=>{if(e.preventDefault(),h.current&&(h.current.open=n),o)return;const s=!n;O||L(s),d?.(s)};return P(()=>{const e=h.current,s=R.current,E=v.current;if(!e||!s||!E)return;if(N.current){N.current=!1,e.open=n;return}if(e.open===n)return;if(t.current?.cancel(),U()||i<=0){e.open=n;return}e.style.overflow="hidden";const S=()=>{e.open=n,e.style.height="",e.style.overflow="",t.current?.cancel(),t.current=null};let y;if(n)e.style.height=`${e.offsetHeight}px`,e.open=!0,y=window.requestAnimationFrame(()=>{const w=`${e.offsetHeight}px`,A=`${s.offsetHeight+E.offsetHeight}px`;t.current=e.animate({height:[w,A]},{duration:i,easing:c,fill:"forwards"}),t.current.addEventListener("finish",S)});else{const w=`${e.offsetHeight}px`,A=`${s.offsetHeight}px`;t.current=e.animate({height:[w,A]},{duration:i,easing:c,fill:"forwards"}),t.current.addEventListener("finish",S)}return()=>{y!==void 0&&window.cancelAnimationFrame(y),t.current?.cancel()}},[n,i,c]),F("details",{...I,"data-disabled":o||void 0,ref:h,children:[F("summary",{"aria-disabled":o,onClick:M,ref:R,style:{cursor:o?void 0:"pointer",listStyle:"none",userSelect:u?"none":void 0,WebkitUserSelect:u?"none":void 0},children:[H,typeof r=="function"?r(n):r]}),x("div",{ref:v,style:{display:"flow-root"},children:g})]})},_=m=>{const{className:g,exclusive:f=!1,items:o}=m,[u,i]=T(null);return x("div",{className:g,children:o.map(({content:c,id:r,onOpenChange:d,...a})=>x(b,{...a,...!f&&d?{onOpenChange:d}:{},...f?{onOpenChange:p=>{i(p?r:null),d?.(p)},open:u===r}:{},children:c},r))})};export{_ as Accordion,b as AccordionItem};
|
package/package.json
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@archoleat/next-accordion",
|
|
3
|
+
"description": "Animated React accordion using details and summary",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": {
|
|
7
|
+
"email": "nikkeyl.me@gmail.com",
|
|
8
|
+
"name": "Archoleat",
|
|
9
|
+
"url": "https://github.com/archoleat"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/archoleat/next-accordion#readme",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/archoleat/next-accordion.git"
|
|
15
|
+
},
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/archoleat/next-accordion/issues"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"accordion",
|
|
21
|
+
"animation",
|
|
22
|
+
"archoleat",
|
|
23
|
+
"details",
|
|
24
|
+
"html-accordion",
|
|
25
|
+
"nextjs",
|
|
26
|
+
"plugin",
|
|
27
|
+
"react-accordion",
|
|
28
|
+
"react",
|
|
29
|
+
"summary",
|
|
30
|
+
"typescript"
|
|
31
|
+
],
|
|
32
|
+
"engines": {
|
|
33
|
+
"bun": ">=1.0.0"
|
|
34
|
+
},
|
|
35
|
+
"type": "module",
|
|
36
|
+
"types": "index.d.ts",
|
|
37
|
+
"imports": {
|
|
38
|
+
"#src/*": "./src/*",
|
|
39
|
+
"#utils/*": "./src/utils/*"
|
|
40
|
+
},
|
|
41
|
+
"exports": {
|
|
42
|
+
".": "./index.js"
|
|
43
|
+
},
|
|
44
|
+
"files": [
|
|
45
|
+
"index.d.ts",
|
|
46
|
+
"index.js"
|
|
47
|
+
],
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"react": ">=18.0.0"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"init": "bun i && husky"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@archoleat/commitlint-define-config": "^1.1.2",
|
|
56
|
+
"@archoleat/prettier-define-config": "^1.2.2",
|
|
57
|
+
"@archoleat/semantic-release-define-config": "^1.3.2",
|
|
58
|
+
"@commitlint/cli": "^21.2.0",
|
|
59
|
+
"@commitlint/config-conventional": "^21.2.0",
|
|
60
|
+
"@commitlint/types": "^21.2.0",
|
|
61
|
+
"@eslint/compat": "^2.1.0",
|
|
62
|
+
"@happy-dom/global-registrator": "^18.0.1",
|
|
63
|
+
"@rollup/plugin-alias": "^6.0.0",
|
|
64
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
65
|
+
"@semantic-release/changelog": "^6.0.3",
|
|
66
|
+
"@semantic-release/git": "^10.0.1",
|
|
67
|
+
"@testing-library/react": "^16.3.0",
|
|
68
|
+
"@types/bun": "^1.3.14",
|
|
69
|
+
"@types/react": "^19.2.17",
|
|
70
|
+
"@types/react-dom": "^19.2.3",
|
|
71
|
+
"@typescript-eslint/eslint-plugin": "^8.62.1",
|
|
72
|
+
"@typescript-eslint/parser": "^8.62.1",
|
|
73
|
+
"conventional-changelog-conventionalcommits": "^10.2.0",
|
|
74
|
+
"editorconfig-checker": "^6.1.1",
|
|
75
|
+
"eslint": "^10.6.0",
|
|
76
|
+
"eslint-config-prettier": "^10.1.8",
|
|
77
|
+
"eslint-define-config": "^2.1.0",
|
|
78
|
+
"eslint-import-resolver-typescript": "^4.4.5",
|
|
79
|
+
"eslint-plugin-import": "^2.32.0",
|
|
80
|
+
"eslint-plugin-react": "^7.37.5",
|
|
81
|
+
"eslint-plugin-react-hooks": "^7.1.1",
|
|
82
|
+
"eslint-plugin-simple-import-sort": "^13.0.0",
|
|
83
|
+
"eslint-plugin-unicorn": "^70.0.0",
|
|
84
|
+
"globals": "^17.7.0",
|
|
85
|
+
"husky": "^9.1.7",
|
|
86
|
+
"lint-staged": "^17.0.8",
|
|
87
|
+
"prettier": "^3.9.4",
|
|
88
|
+
"react": "^19.2.7",
|
|
89
|
+
"react-dom": "^19.2.7",
|
|
90
|
+
"remark": "15.0.1",
|
|
91
|
+
"remark-cli": "^12.0.1",
|
|
92
|
+
"remark-preset-lint-consistent": "^6.0.1",
|
|
93
|
+
"remark-preset-lint-markdown-style-guide": "^6.0.1",
|
|
94
|
+
"remark-preset-lint-recommended": "^7.0.1",
|
|
95
|
+
"rollup": "^4.62.2",
|
|
96
|
+
"rollup-plugin-dts": "^6.4.1",
|
|
97
|
+
"rollup-plugin-esbuild": "^6.2.1",
|
|
98
|
+
"semantic-release": "^25.0.5",
|
|
99
|
+
"tslib": "^2.8.1"
|
|
100
|
+
}
|
|
101
|
+
}
|