Skip to content

Commit 691e572

Browse files
author
metin_sut
committed
first release
0 parents  commit 691e572

15 files changed

+271
-0
lines changed

.babelrc

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"presets": [
3+
"@babel/preset-env",
4+
"@babel/preset-react"
5+
]
6+
}

.gitignore

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*

README.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
## Basic Height Transition Library for React
2+
Simple height transition for collapse elements.
3+
4+
## Installation
5+
This library written with react-hook so you have to install verison of react 16.8.4 or above.
6+
```sh
7+
npm i height-transition -S
8+
```
9+
10+
## Setup
11+
Just connect your button with its ref and pass to props HeightTransition component.
12+
```javascript
13+
import React, { Fragment, useRef } from 'react';
14+
import HeightTransition from 'height-transition';
15+
16+
const App = () => {
17+
const toggleElem = useRef(null);
18+
return (
19+
<Fragment>
20+
<button ref={toggleElem}>Toggle</button>
21+
<HeightTransition
22+
styles={styles}
23+
toggle={toggleElem}
24+
animation="ease-out"
25+
time="100ms"
26+
>
27+
Your Content
28+
</HeightTransition>
29+
</Fragment>
30+
);
31+
}
32+
```
33+
34+
## API
35+
| Api | Description | Default |
36+
| ------ | ------ | ------ |
37+
| styles | Set your own style. |
38+
| toggle | Set your element ref |
39+
| animation | Set your animation style | default : "ease" |
40+
| time | Set your animation time | default : "300ms" |
41+
42+
## Live Example
43+
44+
[![Checkout](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/381vprmr15?fontsize=14)
45+
46+
## License
47+
MIT

dist/heightTransition.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/heightTransition.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/heightTransition.umd.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/heightTransition.umd.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/heightTransition.umd.m.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/heightTransition.umd.m.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/App.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import React, { useState, Fragment, useRef } from 'react';
2+
import HeightTransition from './HeightTransition';
3+
4+
const App = () => {
5+
6+
const data = [
7+
{
8+
name: "Lorem ipsum dolor sit amet consectetur adipisicing elit. Odio, qui!"
9+
},
10+
{
11+
name: "Lorem ipsum dolor sit amet consectetur adipisicing elit. Odio, qui!"
12+
}
13+
];
14+
const dummy = {
15+
name: "Lorem ipsum dolor sit amet consectetur adipisicing elit. Odio, qui!"
16+
};
17+
18+
const [array, setArray] = useState(data);
19+
20+
const pushData = () => {
21+
setArray(state => [...state, dummy]);
22+
};
23+
const resetData = () => {
24+
setArray(data);
25+
};
26+
27+
const styles = {
28+
backgroundColor: '#d1d1d1',
29+
width: '300px'
30+
};
31+
32+
const toggleElem = useRef(null);
33+
34+
return (
35+
<Fragment>
36+
<div>
37+
<button ref={toggleElem}>Toggle</button>
38+
<button onClick={pushData}>Add Data</button>
39+
<button onClick={resetData}>Reset</button>
40+
</div>
41+
<HeightTransition styles={styles} toggle={toggleElem} animation="ease-out" time="100ms">
42+
{array.map((item, key) => (
43+
<p key={key}>{item.name}</p>
44+
))}
45+
</HeightTransition>
46+
</Fragment>
47+
);
48+
}
49+
50+
export default App;

example/HeightTransition.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React, { useRef, useEffect } from "react";
2+
3+
const HeightTransition = props => {
4+
const divElem = useRef(null);
5+
6+
const toggleButton = () => {
7+
divElem.current.style.height = divElem.current.scrollHeight + "px";
8+
if (divElem.current.clientHeight !== 0) {
9+
divElem.current.style.height = "0px";
10+
}
11+
};
12+
13+
useEffect(() => {
14+
divElem.current.style.height = "auto";
15+
divElem.current.style.overflow = "hidden";
16+
divElem.current.style.transition = `height ${props.time ? props.time : "300ms"} ${props.animation ? props.animation : "ease"}`;
17+
window.addEventListener("transitionend", onTransitionend);
18+
props.toggle.current.addEventListener("click", toggleButton);
19+
return () => {
20+
window.removeEventListener("transitionend", onTransitionend);
21+
props.toggle.current.removeEventListener("click", toggleButton);
22+
};
23+
});
24+
25+
const onTransitionend = () => {
26+
if (divElem.current.clientHeight !== 0) {
27+
divElem.current.style.height = "auto";
28+
}
29+
};
30+
31+
return (
32+
<div style={props.styles} ref={divElem}>
33+
{props.children}
34+
</div>
35+
);
36+
};
37+
38+
export default HeightTransition;

example/index.html

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
8+
<title>Document</title>
9+
</head>
10+
11+
<body>
12+
<div id="root"></div>
13+
<script src="./index.js"></script>
14+
</body>
15+
16+
</html>

example/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import App from './App';
4+
5+
ReactDOM.render(<App />, document.getElementById('root'));

package.json

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "height-transition",
3+
"version": "0.2.0",
4+
"description": "Simple height transition for collapse elements.",
5+
"main": "./dist/heightTransition.js",
6+
"umd:main": "dist/heightTransition.umd.js",
7+
"module": "dist/heightTransition.umd.m.js",
8+
"files": [
9+
"dist"
10+
],
11+
"author": "Metin Süt <metindir@gmail.com>",
12+
"homepage": "https://github.com/metinsut/height-transition",
13+
"keywords": [
14+
"react",
15+
"hooks",
16+
"height",
17+
"transition",
18+
"height-transition"
19+
],
20+
"scripts": {
21+
"start": "parcel ./example/index.html --out-dir exampleDist",
22+
"build": "microbundle --jsx React.createElement"
23+
},
24+
"peerDependencies": {
25+
"react": ">=16.8.4"
26+
},
27+
"devDependencies": {
28+
"@babel/core": "^7.3.4",
29+
"@babel/polyfill": "^7.2.5",
30+
"@babel/preset-env": "^7.3.4",
31+
"@babel/preset-react": "^7.0.0",
32+
"babel-core": "^6.26.3",
33+
"babel-eslint": "^10.0.1",
34+
"microbundle": "^0.11.0",
35+
"parcel-bundler": "^1.12.0",
36+
"react": "^16.8.4",
37+
"react-dom": "^16.8.4"
38+
}
39+
}

src/index.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import React, { useRef, useEffect } from "react";
2+
3+
const HeightTransition = props => {
4+
const divElem = useRef(null);
5+
6+
const toggleButton = () => {
7+
divElem.current.style.height = divElem.current.scrollHeight + "px";
8+
if (divElem.current.clientHeight !== 0) {
9+
divElem.current.style.height = "0px";
10+
}
11+
};
12+
13+
useEffect(() => {
14+
divElem.current.style.height = "auto";
15+
divElem.current.style.overflow = "hidden";
16+
divElem.current.style.transition = `height ${props.time ? props.time : "300ms"} ${props.animation ? props.animation : "ease"}`;
17+
window.addEventListener("transitionend", onTransitionend);
18+
props.toggle.current.addEventListener("click", toggleButton);
19+
return () => {
20+
window.removeEventListener("transitionend", onTransitionend);
21+
props.toggle.current.removeEventListener("click", toggleButton);
22+
};
23+
});
24+
25+
const onTransitionend = () => {
26+
if (divElem.current.clientHeight !== 0) {
27+
divElem.current.style.height = "auto";
28+
}
29+
};
30+
31+
return (
32+
<div style={props.styles} ref={divElem}>
33+
{props.children}
34+
</div>
35+
);
36+
};
37+
38+
export default HeightTransition;

0 commit comments

Comments
 (0)