Skip to content

Commit

Permalink
feat(toggle): implement dynamic toggle size function
Browse files Browse the repository at this point in the history
Allows setting a custom toggle function
Allows using toggleSize to toggle the width property.
  • Loading branch information
megheaiulian committed Nov 22, 2022
1 parent e0b59e9 commit 2c663f0
Show file tree
Hide file tree
Showing 9 changed files with 12,100 additions and 7,363 deletions.
19,326 changes: 12,008 additions & 7,318 deletions package-lock.json

Large diffs are not rendered by default.

29 changes: 17 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@
},
"license": "Apache-2.0",
"author": "",
"main": "src/index.js",
"main": "dist/index.js",
"directories": {
"test": "test"
},
"scripts": {
"lint": "eslint --cache --ext .js .",
"lint-tsc": "tsc",
"lint": "tsc && eslint --cache .",
"build": "tsc -p tsconfig.build.json",
"start": "wds",
"test": "wtr --coverage",
"test:watch": "wtr --watch",
"prepare": "husky install",
"storybook:build": "build-storybook",
"storybook:deploy": "storybook-to-ghpages",
"prepare": "husky install"
"storybook:deploy": "storybook-to-ghpages"
},
"release": {
"plugins": [
Expand All @@ -46,23 +46,28 @@
"access": "public"
},
"files": [
"src/*.js"
"dist/"
],
"commitlint": {
"extends": [
"@commitlint/config-conventional"
]
},
"dependencies": {},
"exports": {
".": "./dist/index.js",
"./toggle": "./dist/toggle.js",
"./toggle.js": "./dist/toggle.js"

},
"devDependencies": {
"@commitlint/cli": "^13.0.0",
"@commitlint/config-conventional": "^13.0.0",
"@neovici/eslint-config": "^1.2.0",
"@commitlint/cli": "^17.0.0",
"@commitlint/config-conventional": "^17.0.0",
"@neovici/cfg": "^1.20.1",
"@semantic-release/changelog": "^6.0.0",
"@semantic-release/git": "^10.0.0",
"husky": "^7.0.0",
"husky": "^8.0.0",
"prettier": "^2.5.1",
"semantic-release": "^18.0.0",
"semantic-release": "^19.0.0",
"typescript": "^4.0.0"
}
}
22 changes: 0 additions & 22 deletions src/cosmoz-collapse.js

This file was deleted.

28 changes: 28 additions & 0 deletions src/cosmoz-collapse.ts
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.
11 changes: 0 additions & 11 deletions src/toggle.js

This file was deleted.

27 changes: 27 additions & 0 deletions src/toggle.ts
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",
});
};
9 changes: 9 additions & 0 deletions tsconfig.build.json
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"]
}
11 changes: 11 additions & 0 deletions tsconfig.json
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
}
}

0 comments on commit 2c663f0

Please sign in to comment.