slider-nav-button 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 +167 -0
- package/dist/index.cjs +169 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.mts +39 -0
- package/dist/index.d.ts +39 -0
- package/dist/index.js +142 -0
- package/dist/index.js.map +1 -0
- package/dist/react/index.cjs +173 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.css +157 -0
- package/dist/react/index.css.map +1 -0
- package/dist/react/index.d.mts +13 -0
- package/dist/react/index.d.ts +13 -0
- package/dist/react/index.js +146 -0
- package/dist/react/index.js.map +1 -0
- package/dist/slider-button.css +192 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
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,167 @@
|
|
|
1
|
+
# Slider Nav Button
|
|
2
|
+
|
|
3
|
+
Pill-shaped slider navigation button with divider-to-hover animation. Inspired by [adria.studio](https://www.adria.studio).
|
|
4
|
+
|
|
5
|
+
When you hover over the left or right arrow, a background slides out from the central divider toward the hovered side.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install slider-nav-button
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
For the React version, also install the peer dependencies:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install framer-motion @hugeicons/react @hugeicons/core-free-icons
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
### Vanilla JS
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
// In your HTML: <div id="slider-container"></div>
|
|
25
|
+
|
|
26
|
+
import { SliderButton } from 'slider-nav-button';
|
|
27
|
+
import 'slider-nav-button/styles.css';
|
|
28
|
+
|
|
29
|
+
const button = new SliderButton({
|
|
30
|
+
onPrev: () => console.log('Previous'),
|
|
31
|
+
onNext: () => console.log('Next'),
|
|
32
|
+
size: 'md', // 'sm' | 'md' | 'lg'
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
button.mount('#slider-container');
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### React
|
|
39
|
+
|
|
40
|
+
```jsx
|
|
41
|
+
import { useState } from 'react';
|
|
42
|
+
import { SliderButton } from 'slider-nav-button/react';
|
|
43
|
+
|
|
44
|
+
function Carousel() {
|
|
45
|
+
const [index, setIndex] = useState(0);
|
|
46
|
+
|
|
47
|
+
return (
|
|
48
|
+
<SliderButton
|
|
49
|
+
onPrev={() => setIndex(prev => Math.max(0, prev - 1))}
|
|
50
|
+
onNext={() => setIndex(prev => prev + 1)}
|
|
51
|
+
size="md"
|
|
52
|
+
/>
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
The React version includes styles automatically.
|
|
58
|
+
|
|
59
|
+
### CDN (no bundler)
|
|
60
|
+
|
|
61
|
+
```html
|
|
62
|
+
<div id="container"></div>
|
|
63
|
+
<link rel="stylesheet" href="https://unpkg.com/slider-nav-button/dist/slider-button.css">
|
|
64
|
+
<script type="module">
|
|
65
|
+
import { SliderButton } from 'https://unpkg.com/slider-nav-button/dist/index.js';
|
|
66
|
+
new SliderButton({ onPrev: () => {}, onNext: () => {} }).mount('#container');
|
|
67
|
+
</script>
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Custom icons (React)
|
|
71
|
+
|
|
72
|
+
```jsx
|
|
73
|
+
import { ChevronLeftIcon, ChevronRightIcon } from '@hugeicons/core-free-icons';
|
|
74
|
+
import { HugeiconsIcon } from '@hugeicons/react';
|
|
75
|
+
import { SliderButton } from 'slider-nav-button/react';
|
|
76
|
+
|
|
77
|
+
<SliderButton
|
|
78
|
+
iconLeft={<HugeiconsIcon icon={ChevronLeftIcon} size={18} />}
|
|
79
|
+
iconRight={<HugeiconsIcon icon={ChevronRightIcon} size={18} />}
|
|
80
|
+
onPrev={handlePrev}
|
|
81
|
+
onNext={handleNext}
|
|
82
|
+
/>
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## API
|
|
86
|
+
|
|
87
|
+
### Vanilla JS
|
|
88
|
+
|
|
89
|
+
**Constructor options**
|
|
90
|
+
|
|
91
|
+
| Option | Type | Default | Description |
|
|
92
|
+
|--------|------|---------|-------------|
|
|
93
|
+
| `onPrev` | `() => void` | - | Callback when left arrow is clicked |
|
|
94
|
+
| `onNext` | `() => void` | - | Callback when right arrow is clicked |
|
|
95
|
+
| `iconLeft` | `string` | Default arrow SVG | Custom SVG HTML string for left icon |
|
|
96
|
+
| `iconRight` | `string` | Default arrow SVG | Custom SVG HTML string for right icon |
|
|
97
|
+
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` | Button size |
|
|
98
|
+
| `className` | `string` | - | Additional CSS class for the container |
|
|
99
|
+
|
|
100
|
+
**Methods**
|
|
101
|
+
|
|
102
|
+
- `getElement()` - Returns the root DOM element
|
|
103
|
+
- `mount(parent)` - Mount into a parent element (selector or HTMLElement)
|
|
104
|
+
- `unmount()` - Remove from the DOM
|
|
105
|
+
- `update(options)` - Update options (iconLeft, iconRight, size, className)
|
|
106
|
+
|
|
107
|
+
### React
|
|
108
|
+
|
|
109
|
+
**Props**
|
|
110
|
+
|
|
111
|
+
| Prop | Type | Default | Description |
|
|
112
|
+
|------|------|---------|-------------|
|
|
113
|
+
| `onPrev` | `() => void` | - | Callback when left arrow is clicked |
|
|
114
|
+
| `onNext` | `() => void` | - | Callback when right arrow is clicked |
|
|
115
|
+
| `iconLeft` | `ReactNode` | Default arrow | Custom left icon |
|
|
116
|
+
| `iconRight` | `ReactNode` | Default arrow | Custom right icon |
|
|
117
|
+
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` | Button size |
|
|
118
|
+
| `className` | `string` | - | Additional CSS class |
|
|
119
|
+
|
|
120
|
+
## Customization
|
|
121
|
+
|
|
122
|
+
Use CSS custom properties to theme the component. Target the `.snb` class or pass a custom `className`:
|
|
123
|
+
|
|
124
|
+
```css
|
|
125
|
+
.snb {
|
|
126
|
+
--snb-bg: transparent;
|
|
127
|
+
--snb-hover-bg: #f7f7f7;
|
|
128
|
+
--snb-border-color: #f2f2f2;
|
|
129
|
+
--snb-icon-color: #17181A;
|
|
130
|
+
--snb-divider-color: var(--snb-border-color);
|
|
131
|
+
--snb-radius: 9999px;
|
|
132
|
+
--snb-height: 36px;
|
|
133
|
+
--snb-width: 90px;
|
|
134
|
+
--snb-icon-size: 18px;
|
|
135
|
+
--snb-transition-duration: 0.28s;
|
|
136
|
+
--snb-transition-easing: cubic-bezier(0.25, 0.46, 0.45, 0.94);
|
|
137
|
+
--snb-hover-padding: 2px;
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Dark theme
|
|
142
|
+
|
|
143
|
+
```css
|
|
144
|
+
.dark-container .snb {
|
|
145
|
+
--snb-bg: transparent;
|
|
146
|
+
--snb-hover-bg: #323232;
|
|
147
|
+
--snb-border-color: rgba(255, 255, 255, 0.05);
|
|
148
|
+
--snb-divider-color: rgba(255, 255, 255, 0.05);
|
|
149
|
+
--snb-icon-color: #fff;
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## Demo
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
npm run demo
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Then open the URL shown (e.g. `http://localhost:3000/demo/`).
|
|
160
|
+
|
|
161
|
+
## Browser support
|
|
162
|
+
|
|
163
|
+
Vanilla JS uses `mousemove`/`mouseleave` for hover detection; React uses framer-motion. Supported in all modern browsers (Chrome 105+, Firefox 121+, Safari 15.4+).
|
|
164
|
+
|
|
165
|
+
## License
|
|
166
|
+
|
|
167
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
SliderButton: () => SliderButton
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(src_exports);
|
|
26
|
+
|
|
27
|
+
// src/core/slider-button.ts
|
|
28
|
+
var LEFT_ARROW_SVG = `<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M19 12H5M12 19l-7-7 7-7"/></svg>`;
|
|
29
|
+
var RIGHT_ARROW_SVG = `<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M5 12h14M12 5l7 7-7 7"/></svg>`;
|
|
30
|
+
var SIZE_MAP = {
|
|
31
|
+
sm: { height: 28, width: 70, iconSize: 14 },
|
|
32
|
+
md: { height: 36, width: 90, iconSize: 18 },
|
|
33
|
+
lg: { height: 44, width: 110, iconSize: 22 }
|
|
34
|
+
};
|
|
35
|
+
var SliderButton = class {
|
|
36
|
+
constructor(options = {}) {
|
|
37
|
+
this.hoverSide = null;
|
|
38
|
+
this.prevHoverSide = null;
|
|
39
|
+
this.handleMouseMove = (e) => {
|
|
40
|
+
const rect = this.container.getBoundingClientRect();
|
|
41
|
+
const x = e.clientX - rect.left;
|
|
42
|
+
const mid = rect.width / 2;
|
|
43
|
+
const side = x < mid ? "left" : "right";
|
|
44
|
+
this.prevHoverSide = this.hoverSide;
|
|
45
|
+
this.hoverSide = side;
|
|
46
|
+
this.updateSlideState();
|
|
47
|
+
};
|
|
48
|
+
this.handleMouseLeave = () => {
|
|
49
|
+
this.prevHoverSide = this.hoverSide;
|
|
50
|
+
this.hoverSide = null;
|
|
51
|
+
this.updateSlideState();
|
|
52
|
+
};
|
|
53
|
+
this.options = options;
|
|
54
|
+
const { size = "md" } = options;
|
|
55
|
+
const dimensions = SIZE_MAP[size];
|
|
56
|
+
this.container = document.createElement("div");
|
|
57
|
+
this.container.className = `snb snb-js ${options.className ?? ""}`.trim();
|
|
58
|
+
this.container.style.setProperty("--snb-height", `${dimensions.height}px`);
|
|
59
|
+
this.container.style.setProperty("--snb-width", `${dimensions.width}px`);
|
|
60
|
+
this.container.style.setProperty("--snb-icon-size", `${dimensions.iconSize}px`);
|
|
61
|
+
this.slideBg = document.createElement("div");
|
|
62
|
+
this.slideBg.className = "snb-js-slide-bg";
|
|
63
|
+
this.slideBg.setAttribute("aria-hidden", "true");
|
|
64
|
+
this.leftBtn = document.createElement("button");
|
|
65
|
+
this.leftBtn.className = "snb-left";
|
|
66
|
+
this.leftBtn.type = "button";
|
|
67
|
+
this.leftBtn.setAttribute("aria-label", "Previous");
|
|
68
|
+
this.leftBtn.innerHTML = `<span class="snb-icon">${options.iconLeft ?? LEFT_ARROW_SVG}</span>`;
|
|
69
|
+
const divider = document.createElement("div");
|
|
70
|
+
divider.className = "snb-divider";
|
|
71
|
+
divider.setAttribute("aria-hidden", "true");
|
|
72
|
+
this.rightBtn = document.createElement("button");
|
|
73
|
+
this.rightBtn.className = "snb-right";
|
|
74
|
+
this.rightBtn.type = "button";
|
|
75
|
+
this.rightBtn.setAttribute("aria-label", "Next");
|
|
76
|
+
this.rightBtn.innerHTML = `<span class="snb-icon">${options.iconRight ?? RIGHT_ARROW_SVG}</span>`;
|
|
77
|
+
this.container.append(this.slideBg, this.leftBtn, divider, this.rightBtn);
|
|
78
|
+
this.leftBtn.addEventListener("click", () => this.options.onPrev?.());
|
|
79
|
+
this.rightBtn.addEventListener("click", () => this.options.onNext?.());
|
|
80
|
+
this.container.addEventListener("mousemove", this.handleMouseMove);
|
|
81
|
+
this.container.addEventListener("mouseleave", this.handleMouseLeave);
|
|
82
|
+
}
|
|
83
|
+
updateSlideState() {
|
|
84
|
+
const isEnter = this.prevHoverSide === null && this.hoverSide !== null;
|
|
85
|
+
this.container.classList.remove(
|
|
86
|
+
"snb-hover-left",
|
|
87
|
+
"snb-hover-right",
|
|
88
|
+
"snb-exit-from-left",
|
|
89
|
+
"snb-exit-from-right",
|
|
90
|
+
"snb-enter-right-prep"
|
|
91
|
+
);
|
|
92
|
+
if (this.hoverSide === "left") {
|
|
93
|
+
this.container.classList.add("snb-hover-left");
|
|
94
|
+
} else if (this.hoverSide === "right") {
|
|
95
|
+
if (isEnter) {
|
|
96
|
+
this.container.classList.add("snb-enter-right-prep");
|
|
97
|
+
requestAnimationFrame(() => {
|
|
98
|
+
requestAnimationFrame(() => {
|
|
99
|
+
this.container.classList.remove("snb-enter-right-prep");
|
|
100
|
+
this.container.classList.add("snb-hover-right");
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
} else {
|
|
104
|
+
this.container.classList.add("snb-hover-right");
|
|
105
|
+
}
|
|
106
|
+
} else if (this.prevHoverSide === "left") {
|
|
107
|
+
this.container.classList.add("snb-exit-from-left");
|
|
108
|
+
this.slideBg.addEventListener("transitionend", () => {
|
|
109
|
+
this.container.classList.remove("snb-exit-from-left");
|
|
110
|
+
}, { once: true });
|
|
111
|
+
} else if (this.prevHoverSide === "right") {
|
|
112
|
+
this.container.classList.add("snb-exit-from-right");
|
|
113
|
+
this.slideBg.addEventListener("transitionend", () => {
|
|
114
|
+
this.container.classList.remove("snb-exit-from-right");
|
|
115
|
+
}, { once: true });
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Get the root DOM element
|
|
120
|
+
*/
|
|
121
|
+
getElement() {
|
|
122
|
+
return this.container;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Mount the button into a parent element
|
|
126
|
+
*/
|
|
127
|
+
mount(parent) {
|
|
128
|
+
const el = typeof parent === "string" ? document.querySelector(parent) : parent;
|
|
129
|
+
if (!el) throw new Error(`SliderButton: parent element not found`);
|
|
130
|
+
el.appendChild(this.container);
|
|
131
|
+
return this;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Remove the button from the DOM
|
|
135
|
+
*/
|
|
136
|
+
unmount() {
|
|
137
|
+
this.container.removeEventListener("mousemove", this.handleMouseMove);
|
|
138
|
+
this.container.removeEventListener("mouseleave", this.handleMouseLeave);
|
|
139
|
+
this.container.remove();
|
|
140
|
+
return this;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Update options
|
|
144
|
+
*/
|
|
145
|
+
update(options) {
|
|
146
|
+
this.options = { ...this.options, ...options };
|
|
147
|
+
if (options.iconLeft !== void 0) {
|
|
148
|
+
this.leftBtn.innerHTML = `<span class="snb-icon">${options.iconLeft}</span>`;
|
|
149
|
+
}
|
|
150
|
+
if (options.iconRight !== void 0) {
|
|
151
|
+
this.rightBtn.innerHTML = `<span class="snb-icon">${options.iconRight}</span>`;
|
|
152
|
+
}
|
|
153
|
+
if (options.size !== void 0) {
|
|
154
|
+
const dimensions = SIZE_MAP[options.size];
|
|
155
|
+
this.container.style.setProperty("--snb-height", `${dimensions.height}px`);
|
|
156
|
+
this.container.style.setProperty("--snb-width", `${dimensions.width}px`);
|
|
157
|
+
this.container.style.setProperty("--snb-icon-size", `${dimensions.iconSize}px`);
|
|
158
|
+
}
|
|
159
|
+
if (options.className !== void 0) {
|
|
160
|
+
this.container.className = `snb snb-js ${options.className ?? ""}`.trim();
|
|
161
|
+
}
|
|
162
|
+
return this;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
166
|
+
0 && (module.exports = {
|
|
167
|
+
SliderButton
|
|
168
|
+
});
|
|
169
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/core/slider-button.ts"],"sourcesContent":["export { SliderButton } from './core/slider-button';\nexport type { SliderButtonOptions } from './core/slider-button';\n","const LEFT_ARROW_SVG = `<svg viewBox=\"0 0 24 24\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M19 12H5M12 19l-7-7 7-7\"/></svg>`;\nconst RIGHT_ARROW_SVG = `<svg viewBox=\"0 0 24 24\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M5 12h14M12 5l7 7-7 7\"/></svg>`;\n\nexport interface SliderButtonOptions {\n onPrev?: () => void;\n onNext?: () => void;\n iconLeft?: string;\n iconRight?: string;\n size?: 'sm' | 'md' | 'lg';\n className?: string;\n}\n\nconst SIZE_MAP = {\n sm: { height: 28, width: 70, iconSize: 14 },\n md: { height: 36, width: 90, iconSize: 18 },\n lg: { height: 44, width: 110, iconSize: 22 },\n};\n\ntype HoverSide = 'left' | 'right' | null;\n\nexport class SliderButton {\n private container: HTMLDivElement;\n private leftBtn: HTMLButtonElement;\n private rightBtn: HTMLButtonElement;\n private options: SliderButtonOptions;\n private hoverSide: HoverSide = null;\n private prevHoverSide: HoverSide = null;\n private slideBg: HTMLDivElement;\n\n constructor(options: SliderButtonOptions = {}) {\n this.options = options;\n const { size = 'md' } = options;\n const dimensions = SIZE_MAP[size];\n\n this.container = document.createElement('div');\n this.container.className = `snb snb-js ${options.className ?? ''}`.trim();\n this.container.style.setProperty('--snb-height', `${dimensions.height}px`);\n this.container.style.setProperty('--snb-width', `${dimensions.width}px`);\n this.container.style.setProperty('--snb-icon-size', `${dimensions.iconSize}px`);\n\n this.slideBg = document.createElement('div');\n this.slideBg.className = 'snb-js-slide-bg';\n this.slideBg.setAttribute('aria-hidden', 'true');\n\n this.leftBtn = document.createElement('button');\n this.leftBtn.className = 'snb-left';\n this.leftBtn.type = 'button';\n this.leftBtn.setAttribute('aria-label', 'Previous');\n this.leftBtn.innerHTML = `<span class=\"snb-icon\">${options.iconLeft ?? LEFT_ARROW_SVG}</span>`;\n\n const divider = document.createElement('div');\n divider.className = 'snb-divider';\n divider.setAttribute('aria-hidden', 'true');\n\n this.rightBtn = document.createElement('button');\n this.rightBtn.className = 'snb-right';\n this.rightBtn.type = 'button';\n this.rightBtn.setAttribute('aria-label', 'Next');\n this.rightBtn.innerHTML = `<span class=\"snb-icon\">${options.iconRight ?? RIGHT_ARROW_SVG}</span>`;\n\n this.container.append(this.slideBg, this.leftBtn, divider, this.rightBtn);\n\n this.leftBtn.addEventListener('click', () => this.options.onPrev?.());\n this.rightBtn.addEventListener('click', () => this.options.onNext?.());\n\n this.container.addEventListener('mousemove', this.handleMouseMove);\n this.container.addEventListener('mouseleave', this.handleMouseLeave);\n }\n\n private handleMouseMove = (e: MouseEvent): void => {\n const rect = this.container.getBoundingClientRect();\n const x = e.clientX - rect.left;\n const mid = rect.width / 2;\n const side: HoverSide = x < mid ? 'left' : 'right';\n this.prevHoverSide = this.hoverSide;\n this.hoverSide = side;\n this.updateSlideState();\n };\n\n private handleMouseLeave = (): void => {\n this.prevHoverSide = this.hoverSide;\n this.hoverSide = null;\n this.updateSlideState();\n };\n\n private updateSlideState(): void {\n const isEnter = this.prevHoverSide === null && this.hoverSide !== null;\n\n this.container.classList.remove(\n 'snb-hover-left', 'snb-hover-right', 'snb-exit-from-left', 'snb-exit-from-right', 'snb-enter-right-prep'\n );\n\n if (this.hoverSide === 'left') {\n this.container.classList.add('snb-hover-left');\n } else if (this.hoverSide === 'right') {\n if (isEnter) {\n this.container.classList.add('snb-enter-right-prep');\n requestAnimationFrame(() => {\n requestAnimationFrame(() => {\n this.container.classList.remove('snb-enter-right-prep');\n this.container.classList.add('snb-hover-right');\n });\n });\n } else {\n this.container.classList.add('snb-hover-right');\n }\n } else if (this.prevHoverSide === 'left') {\n this.container.classList.add('snb-exit-from-left');\n this.slideBg.addEventListener('transitionend', () => {\n this.container.classList.remove('snb-exit-from-left');\n }, { once: true });\n } else if (this.prevHoverSide === 'right') {\n this.container.classList.add('snb-exit-from-right');\n this.slideBg.addEventListener('transitionend', () => {\n this.container.classList.remove('snb-exit-from-right');\n }, { once: true });\n }\n }\n\n /**\n * Get the root DOM element\n */\n getElement(): HTMLDivElement {\n return this.container;\n }\n\n /**\n * Mount the button into a parent element\n */\n mount(parent: HTMLElement | string): this {\n const el = typeof parent === 'string' ? document.querySelector(parent) : parent;\n if (!el) throw new Error(`SliderButton: parent element not found`);\n el.appendChild(this.container);\n return this;\n }\n\n /**\n * Remove the button from the DOM\n */\n unmount(): this {\n this.container.removeEventListener('mousemove', this.handleMouseMove);\n this.container.removeEventListener('mouseleave', this.handleMouseLeave);\n this.container.remove();\n return this;\n }\n\n /**\n * Update options\n */\n update(options: Partial<SliderButtonOptions>): this {\n this.options = { ...this.options, ...options };\n if (options.iconLeft !== undefined) {\n this.leftBtn.innerHTML = `<span class=\"snb-icon\">${options.iconLeft}</span>`;\n }\n if (options.iconRight !== undefined) {\n this.rightBtn.innerHTML = `<span class=\"snb-icon\">${options.iconRight}</span>`;\n }\n if (options.size !== undefined) {\n const dimensions = SIZE_MAP[options.size];\n this.container.style.setProperty('--snb-height', `${dimensions.height}px`);\n this.container.style.setProperty('--snb-width', `${dimensions.width}px`);\n this.container.style.setProperty('--snb-icon-size', `${dimensions.iconSize}px`);\n }\n if (options.className !== undefined) {\n this.container.className = `snb snb-js ${options.className ?? ''}`.trim();\n }\n return this;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAM,iBAAiB;AACvB,IAAM,kBAAkB;AAWxB,IAAM,WAAW;AAAA,EACf,IAAI,EAAE,QAAQ,IAAI,OAAO,IAAI,UAAU,GAAG;AAAA,EAC1C,IAAI,EAAE,QAAQ,IAAI,OAAO,IAAI,UAAU,GAAG;AAAA,EAC1C,IAAI,EAAE,QAAQ,IAAI,OAAO,KAAK,UAAU,GAAG;AAC7C;AAIO,IAAM,eAAN,MAAmB;AAAA,EASxB,YAAY,UAA+B,CAAC,GAAG;AAJ/C,SAAQ,YAAuB;AAC/B,SAAQ,gBAA2B;AA2CnC,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
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
interface SliderButtonOptions {
|
|
2
|
+
onPrev?: () => void;
|
|
3
|
+
onNext?: () => void;
|
|
4
|
+
iconLeft?: string;
|
|
5
|
+
iconRight?: string;
|
|
6
|
+
size?: 'sm' | 'md' | 'lg';
|
|
7
|
+
className?: string;
|
|
8
|
+
}
|
|
9
|
+
declare class SliderButton {
|
|
10
|
+
private container;
|
|
11
|
+
private leftBtn;
|
|
12
|
+
private rightBtn;
|
|
13
|
+
private options;
|
|
14
|
+
private hoverSide;
|
|
15
|
+
private prevHoverSide;
|
|
16
|
+
private slideBg;
|
|
17
|
+
constructor(options?: SliderButtonOptions);
|
|
18
|
+
private handleMouseMove;
|
|
19
|
+
private handleMouseLeave;
|
|
20
|
+
private updateSlideState;
|
|
21
|
+
/**
|
|
22
|
+
* Get the root DOM element
|
|
23
|
+
*/
|
|
24
|
+
getElement(): HTMLDivElement;
|
|
25
|
+
/**
|
|
26
|
+
* Mount the button into a parent element
|
|
27
|
+
*/
|
|
28
|
+
mount(parent: HTMLElement | string): this;
|
|
29
|
+
/**
|
|
30
|
+
* Remove the button from the DOM
|
|
31
|
+
*/
|
|
32
|
+
unmount(): this;
|
|
33
|
+
/**
|
|
34
|
+
* Update options
|
|
35
|
+
*/
|
|
36
|
+
update(options: Partial<SliderButtonOptions>): this;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export { SliderButton, type SliderButtonOptions };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
interface SliderButtonOptions {
|
|
2
|
+
onPrev?: () => void;
|
|
3
|
+
onNext?: () => void;
|
|
4
|
+
iconLeft?: string;
|
|
5
|
+
iconRight?: string;
|
|
6
|
+
size?: 'sm' | 'md' | 'lg';
|
|
7
|
+
className?: string;
|
|
8
|
+
}
|
|
9
|
+
declare class SliderButton {
|
|
10
|
+
private container;
|
|
11
|
+
private leftBtn;
|
|
12
|
+
private rightBtn;
|
|
13
|
+
private options;
|
|
14
|
+
private hoverSide;
|
|
15
|
+
private prevHoverSide;
|
|
16
|
+
private slideBg;
|
|
17
|
+
constructor(options?: SliderButtonOptions);
|
|
18
|
+
private handleMouseMove;
|
|
19
|
+
private handleMouseLeave;
|
|
20
|
+
private updateSlideState;
|
|
21
|
+
/**
|
|
22
|
+
* Get the root DOM element
|
|
23
|
+
*/
|
|
24
|
+
getElement(): HTMLDivElement;
|
|
25
|
+
/**
|
|
26
|
+
* Mount the button into a parent element
|
|
27
|
+
*/
|
|
28
|
+
mount(parent: HTMLElement | string): this;
|
|
29
|
+
/**
|
|
30
|
+
* Remove the button from the DOM
|
|
31
|
+
*/
|
|
32
|
+
unmount(): this;
|
|
33
|
+
/**
|
|
34
|
+
* Update options
|
|
35
|
+
*/
|
|
36
|
+
update(options: Partial<SliderButtonOptions>): this;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export { SliderButton, type SliderButtonOptions };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// src/core/slider-button.ts
|
|
2
|
+
var LEFT_ARROW_SVG = `<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M19 12H5M12 19l-7-7 7-7"/></svg>`;
|
|
3
|
+
var RIGHT_ARROW_SVG = `<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M5 12h14M12 5l7 7-7 7"/></svg>`;
|
|
4
|
+
var SIZE_MAP = {
|
|
5
|
+
sm: { height: 28, width: 70, iconSize: 14 },
|
|
6
|
+
md: { height: 36, width: 90, iconSize: 18 },
|
|
7
|
+
lg: { height: 44, width: 110, iconSize: 22 }
|
|
8
|
+
};
|
|
9
|
+
var SliderButton = class {
|
|
10
|
+
constructor(options = {}) {
|
|
11
|
+
this.hoverSide = null;
|
|
12
|
+
this.prevHoverSide = null;
|
|
13
|
+
this.handleMouseMove = (e) => {
|
|
14
|
+
const rect = this.container.getBoundingClientRect();
|
|
15
|
+
const x = e.clientX - rect.left;
|
|
16
|
+
const mid = rect.width / 2;
|
|
17
|
+
const side = x < mid ? "left" : "right";
|
|
18
|
+
this.prevHoverSide = this.hoverSide;
|
|
19
|
+
this.hoverSide = side;
|
|
20
|
+
this.updateSlideState();
|
|
21
|
+
};
|
|
22
|
+
this.handleMouseLeave = () => {
|
|
23
|
+
this.prevHoverSide = this.hoverSide;
|
|
24
|
+
this.hoverSide = null;
|
|
25
|
+
this.updateSlideState();
|
|
26
|
+
};
|
|
27
|
+
this.options = options;
|
|
28
|
+
const { size = "md" } = options;
|
|
29
|
+
const dimensions = SIZE_MAP[size];
|
|
30
|
+
this.container = document.createElement("div");
|
|
31
|
+
this.container.className = `snb snb-js ${options.className ?? ""}`.trim();
|
|
32
|
+
this.container.style.setProperty("--snb-height", `${dimensions.height}px`);
|
|
33
|
+
this.container.style.setProperty("--snb-width", `${dimensions.width}px`);
|
|
34
|
+
this.container.style.setProperty("--snb-icon-size", `${dimensions.iconSize}px`);
|
|
35
|
+
this.slideBg = document.createElement("div");
|
|
36
|
+
this.slideBg.className = "snb-js-slide-bg";
|
|
37
|
+
this.slideBg.setAttribute("aria-hidden", "true");
|
|
38
|
+
this.leftBtn = document.createElement("button");
|
|
39
|
+
this.leftBtn.className = "snb-left";
|
|
40
|
+
this.leftBtn.type = "button";
|
|
41
|
+
this.leftBtn.setAttribute("aria-label", "Previous");
|
|
42
|
+
this.leftBtn.innerHTML = `<span class="snb-icon">${options.iconLeft ?? LEFT_ARROW_SVG}</span>`;
|
|
43
|
+
const divider = document.createElement("div");
|
|
44
|
+
divider.className = "snb-divider";
|
|
45
|
+
divider.setAttribute("aria-hidden", "true");
|
|
46
|
+
this.rightBtn = document.createElement("button");
|
|
47
|
+
this.rightBtn.className = "snb-right";
|
|
48
|
+
this.rightBtn.type = "button";
|
|
49
|
+
this.rightBtn.setAttribute("aria-label", "Next");
|
|
50
|
+
this.rightBtn.innerHTML = `<span class="snb-icon">${options.iconRight ?? RIGHT_ARROW_SVG}</span>`;
|
|
51
|
+
this.container.append(this.slideBg, this.leftBtn, divider, this.rightBtn);
|
|
52
|
+
this.leftBtn.addEventListener("click", () => this.options.onPrev?.());
|
|
53
|
+
this.rightBtn.addEventListener("click", () => this.options.onNext?.());
|
|
54
|
+
this.container.addEventListener("mousemove", this.handleMouseMove);
|
|
55
|
+
this.container.addEventListener("mouseleave", this.handleMouseLeave);
|
|
56
|
+
}
|
|
57
|
+
updateSlideState() {
|
|
58
|
+
const isEnter = this.prevHoverSide === null && this.hoverSide !== null;
|
|
59
|
+
this.container.classList.remove(
|
|
60
|
+
"snb-hover-left",
|
|
61
|
+
"snb-hover-right",
|
|
62
|
+
"snb-exit-from-left",
|
|
63
|
+
"snb-exit-from-right",
|
|
64
|
+
"snb-enter-right-prep"
|
|
65
|
+
);
|
|
66
|
+
if (this.hoverSide === "left") {
|
|
67
|
+
this.container.classList.add("snb-hover-left");
|
|
68
|
+
} else if (this.hoverSide === "right") {
|
|
69
|
+
if (isEnter) {
|
|
70
|
+
this.container.classList.add("snb-enter-right-prep");
|
|
71
|
+
requestAnimationFrame(() => {
|
|
72
|
+
requestAnimationFrame(() => {
|
|
73
|
+
this.container.classList.remove("snb-enter-right-prep");
|
|
74
|
+
this.container.classList.add("snb-hover-right");
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
} else {
|
|
78
|
+
this.container.classList.add("snb-hover-right");
|
|
79
|
+
}
|
|
80
|
+
} else if (this.prevHoverSide === "left") {
|
|
81
|
+
this.container.classList.add("snb-exit-from-left");
|
|
82
|
+
this.slideBg.addEventListener("transitionend", () => {
|
|
83
|
+
this.container.classList.remove("snb-exit-from-left");
|
|
84
|
+
}, { once: true });
|
|
85
|
+
} else if (this.prevHoverSide === "right") {
|
|
86
|
+
this.container.classList.add("snb-exit-from-right");
|
|
87
|
+
this.slideBg.addEventListener("transitionend", () => {
|
|
88
|
+
this.container.classList.remove("snb-exit-from-right");
|
|
89
|
+
}, { once: true });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Get the root DOM element
|
|
94
|
+
*/
|
|
95
|
+
getElement() {
|
|
96
|
+
return this.container;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Mount the button into a parent element
|
|
100
|
+
*/
|
|
101
|
+
mount(parent) {
|
|
102
|
+
const el = typeof parent === "string" ? document.querySelector(parent) : parent;
|
|
103
|
+
if (!el) throw new Error(`SliderButton: parent element not found`);
|
|
104
|
+
el.appendChild(this.container);
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Remove the button from the DOM
|
|
109
|
+
*/
|
|
110
|
+
unmount() {
|
|
111
|
+
this.container.removeEventListener("mousemove", this.handleMouseMove);
|
|
112
|
+
this.container.removeEventListener("mouseleave", this.handleMouseLeave);
|
|
113
|
+
this.container.remove();
|
|
114
|
+
return this;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Update options
|
|
118
|
+
*/
|
|
119
|
+
update(options) {
|
|
120
|
+
this.options = { ...this.options, ...options };
|
|
121
|
+
if (options.iconLeft !== void 0) {
|
|
122
|
+
this.leftBtn.innerHTML = `<span class="snb-icon">${options.iconLeft}</span>`;
|
|
123
|
+
}
|
|
124
|
+
if (options.iconRight !== void 0) {
|
|
125
|
+
this.rightBtn.innerHTML = `<span class="snb-icon">${options.iconRight}</span>`;
|
|
126
|
+
}
|
|
127
|
+
if (options.size !== void 0) {
|
|
128
|
+
const dimensions = SIZE_MAP[options.size];
|
|
129
|
+
this.container.style.setProperty("--snb-height", `${dimensions.height}px`);
|
|
130
|
+
this.container.style.setProperty("--snb-width", `${dimensions.width}px`);
|
|
131
|
+
this.container.style.setProperty("--snb-icon-size", `${dimensions.iconSize}px`);
|
|
132
|
+
}
|
|
133
|
+
if (options.className !== void 0) {
|
|
134
|
+
this.container.className = `snb snb-js ${options.className ?? ""}`.trim();
|
|
135
|
+
}
|
|
136
|
+
return this;
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
export {
|
|
140
|
+
SliderButton
|
|
141
|
+
};
|
|
142
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/core/slider-button.ts"],"sourcesContent":["const LEFT_ARROW_SVG = `<svg viewBox=\"0 0 24 24\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M19 12H5M12 19l-7-7 7-7\"/></svg>`;\nconst RIGHT_ARROW_SVG = `<svg viewBox=\"0 0 24 24\" xmlns=\"http://www.w3.org/2000/svg\"><path d=\"M5 12h14M12 5l7 7-7 7\"/></svg>`;\n\nexport interface SliderButtonOptions {\n onPrev?: () => void;\n onNext?: () => void;\n iconLeft?: string;\n iconRight?: string;\n size?: 'sm' | 'md' | 'lg';\n className?: string;\n}\n\nconst SIZE_MAP = {\n sm: { height: 28, width: 70, iconSize: 14 },\n md: { height: 36, width: 90, iconSize: 18 },\n lg: { height: 44, width: 110, iconSize: 22 },\n};\n\ntype HoverSide = 'left' | 'right' | null;\n\nexport class SliderButton {\n private container: HTMLDivElement;\n private leftBtn: HTMLButtonElement;\n private rightBtn: HTMLButtonElement;\n private options: SliderButtonOptions;\n private hoverSide: HoverSide = null;\n private prevHoverSide: HoverSide = null;\n private slideBg: HTMLDivElement;\n\n constructor(options: SliderButtonOptions = {}) {\n this.options = options;\n const { size = 'md' } = options;\n const dimensions = SIZE_MAP[size];\n\n this.container = document.createElement('div');\n this.container.className = `snb snb-js ${options.className ?? ''}`.trim();\n this.container.style.setProperty('--snb-height', `${dimensions.height}px`);\n this.container.style.setProperty('--snb-width', `${dimensions.width}px`);\n this.container.style.setProperty('--snb-icon-size', `${dimensions.iconSize}px`);\n\n this.slideBg = document.createElement('div');\n this.slideBg.className = 'snb-js-slide-bg';\n this.slideBg.setAttribute('aria-hidden', 'true');\n\n this.leftBtn = document.createElement('button');\n this.leftBtn.className = 'snb-left';\n this.leftBtn.type = 'button';\n this.leftBtn.setAttribute('aria-label', 'Previous');\n this.leftBtn.innerHTML = `<span class=\"snb-icon\">${options.iconLeft ?? LEFT_ARROW_SVG}</span>`;\n\n const divider = document.createElement('div');\n divider.className = 'snb-divider';\n divider.setAttribute('aria-hidden', 'true');\n\n this.rightBtn = document.createElement('button');\n this.rightBtn.className = 'snb-right';\n this.rightBtn.type = 'button';\n this.rightBtn.setAttribute('aria-label', 'Next');\n this.rightBtn.innerHTML = `<span class=\"snb-icon\">${options.iconRight ?? RIGHT_ARROW_SVG}</span>`;\n\n this.container.append(this.slideBg, this.leftBtn, divider, this.rightBtn);\n\n this.leftBtn.addEventListener('click', () => this.options.onPrev?.());\n this.rightBtn.addEventListener('click', () => this.options.onNext?.());\n\n this.container.addEventListener('mousemove', this.handleMouseMove);\n this.container.addEventListener('mouseleave', this.handleMouseLeave);\n }\n\n private handleMouseMove = (e: MouseEvent): void => {\n const rect = this.container.getBoundingClientRect();\n const x = e.clientX - rect.left;\n const mid = rect.width / 2;\n const side: HoverSide = x < mid ? 'left' : 'right';\n this.prevHoverSide = this.hoverSide;\n this.hoverSide = side;\n this.updateSlideState();\n };\n\n private handleMouseLeave = (): void => {\n this.prevHoverSide = this.hoverSide;\n this.hoverSide = null;\n this.updateSlideState();\n };\n\n private updateSlideState(): void {\n const isEnter = this.prevHoverSide === null && this.hoverSide !== null;\n\n this.container.classList.remove(\n 'snb-hover-left', 'snb-hover-right', 'snb-exit-from-left', 'snb-exit-from-right', 'snb-enter-right-prep'\n );\n\n if (this.hoverSide === 'left') {\n this.container.classList.add('snb-hover-left');\n } else if (this.hoverSide === 'right') {\n if (isEnter) {\n this.container.classList.add('snb-enter-right-prep');\n requestAnimationFrame(() => {\n requestAnimationFrame(() => {\n this.container.classList.remove('snb-enter-right-prep');\n this.container.classList.add('snb-hover-right');\n });\n });\n } else {\n this.container.classList.add('snb-hover-right');\n }\n } else if (this.prevHoverSide === 'left') {\n this.container.classList.add('snb-exit-from-left');\n this.slideBg.addEventListener('transitionend', () => {\n this.container.classList.remove('snb-exit-from-left');\n }, { once: true });\n } else if (this.prevHoverSide === 'right') {\n this.container.classList.add('snb-exit-from-right');\n this.slideBg.addEventListener('transitionend', () => {\n this.container.classList.remove('snb-exit-from-right');\n }, { once: true });\n }\n }\n\n /**\n * Get the root DOM element\n */\n getElement(): HTMLDivElement {\n return this.container;\n }\n\n /**\n * Mount the button into a parent element\n */\n mount(parent: HTMLElement | string): this {\n const el = typeof parent === 'string' ? document.querySelector(parent) : parent;\n if (!el) throw new Error(`SliderButton: parent element not found`);\n el.appendChild(this.container);\n return this;\n }\n\n /**\n * Remove the button from the DOM\n */\n unmount(): this {\n this.container.removeEventListener('mousemove', this.handleMouseMove);\n this.container.removeEventListener('mouseleave', this.handleMouseLeave);\n this.container.remove();\n return this;\n }\n\n /**\n * Update options\n */\n update(options: Partial<SliderButtonOptions>): this {\n this.options = { ...this.options, ...options };\n if (options.iconLeft !== undefined) {\n this.leftBtn.innerHTML = `<span class=\"snb-icon\">${options.iconLeft}</span>`;\n }\n if (options.iconRight !== undefined) {\n this.rightBtn.innerHTML = `<span class=\"snb-icon\">${options.iconRight}</span>`;\n }\n if (options.size !== undefined) {\n const dimensions = SIZE_MAP[options.size];\n this.container.style.setProperty('--snb-height', `${dimensions.height}px`);\n this.container.style.setProperty('--snb-width', `${dimensions.width}px`);\n this.container.style.setProperty('--snb-icon-size', `${dimensions.iconSize}px`);\n }\n if (options.className !== undefined) {\n this.container.className = `snb snb-js ${options.className ?? ''}`.trim();\n }\n return this;\n }\n}\n"],"mappings":";AAAA,IAAM,iBAAiB;AACvB,IAAM,kBAAkB;AAWxB,IAAM,WAAW;AAAA,EACf,IAAI,EAAE,QAAQ,IAAI,OAAO,IAAI,UAAU,GAAG;AAAA,EAC1C,IAAI,EAAE,QAAQ,IAAI,OAAO,IAAI,UAAU,GAAG;AAAA,EAC1C,IAAI,EAAE,QAAQ,IAAI,OAAO,KAAK,UAAU,GAAG;AAC7C;AAIO,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