-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(toggle): implement dynamic toggle size function
Allows setting a custom toggle function Allows using toggleSize to toggle the width property.
- Loading branch information
1 parent
e0b59e9
commit 2c663f0
Showing
9 changed files
with
12,100 additions
and
7,363 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { toggleSize } from "./toggle"; | ||
|
||
class Collapse extends HTMLElement { | ||
static get observedAttributes() { | ||
return ["opened"]; | ||
} | ||
|
||
toggle = toggleSize("height"); | ||
|
||
constructor() { | ||
super(); | ||
Object.assign(this.style, { display: "none", overflow: "hidden" }); | ||
} | ||
|
||
attributeChangedCallback(name: string, prevValue?: unknown, value?: unknown) { | ||
switch (name) { | ||
case "opened": { | ||
const visible = value != null; | ||
return this.isConnected | ||
? this.toggle(this, visible) | ||
: Object.assign(this.style, { display: visible ? "" : "none" }); | ||
} | ||
} | ||
} | ||
} | ||
|
||
export { Collapse }; | ||
customElements.define("cosmoz-collapse", Collapse); |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const defaults: Partial<KeyframeAnimationOptions> = { duration: 250 }; | ||
|
||
export type SizeP = "width" | "height"; | ||
|
||
export const toggleSize = | ||
(prop: SizeP) => | ||
<T extends HTMLElement>( | ||
el: T, | ||
visible?: boolean, | ||
opts?: Partial<KeyframeAnimationOptions> | ||
) => { | ||
const maxSizeProp = "max" + prop.charAt(0).toUpperCase() + prop.slice(1); | ||
Object.assign(el.style, { [maxSizeProp]: "", display: "block" }); | ||
const { [prop]: size } = el.getBoundingClientRect(), | ||
kf = [0, size], | ||
[from, to] = visible ? kf : kf.slice().reverse(), | ||
anim = el.animate( | ||
[{ [maxSizeProp]: `${from}px` }, { [maxSizeProp]: `${to}px` }], | ||
{ ...defaults, ...opts } | ||
); | ||
|
||
anim.onfinish = () => | ||
Object.assign(el.style, { | ||
[maxSizeProp]: "", | ||
display: visible ? "" : "none", | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"outDir": "dist", | ||
"declaration": true, | ||
"noEmit": false | ||
}, | ||
"include": ["src"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"compilerOptions": { | ||
"allowSyntheticDefaultImports": true, | ||
"noEmit": true, | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"strict": true, | ||
"target": "esnext", | ||
"allowJs": true | ||
} | ||
} |