Skip to content

Commit c80b4ef

Browse files
committed
feature:remove all dependencies
1 parent f22bb04 commit c80b4ef

File tree

5 files changed

+60
-88
lines changed

5 files changed

+60
-88
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# react-swim-button
22

3+
![version](https://img.shields.io/github/package-json/v/happy-func/react-swim-button/main) ![typescript](https://img.shields.io/npm/types/react-swim-button) ![esm](https://img.shields.io/static/v1?label=build&message=esm&color=blue) ![npm bundle size (version)](https://img.shields.io/bundlephobia/min/react-swim-button/latest) ![GitHub Repo stars](https://img.shields.io/github/stars/happy-func/sudoku.js?style=social)
4+
5+
Zero dependencies
6+
37
## Getting Started
48

59
Install dependencies,
@@ -18,6 +22,7 @@ $ yarn add react-swim-button
1822
```tsx
1923
import React, { useState } from 'react';
2024
import ReactSwimButton from 'react-swim-button';
25+
import 'react-swim-button/es/style';
2126

2227
const texts = ['React', 'Swim', 'Button'];
2328

@@ -46,6 +51,7 @@ export default () => {
4651
```tsx
4752
import React, { useState } from 'react';
4853
import ReactSwimButton from 'react-swim-button';
54+
import 'react-swim-button/es/style';
4955

5056
export default () => {
5157
const [activeAt, setActiveAt] = useState(0);
@@ -71,6 +77,7 @@ export default () => {
7177
```tsx
7278
import React, { useState } from 'react';
7379
import ReactSwimButton from 'react-swim-button';
80+
import 'react-swim-button/es/style';
7481

7582
const texts = [
7683
{

docs/demo/plainbutton.less

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,20 @@
1515
box-shadow: none;
1616

1717
&:before {
18+
background-color: var(--primary-color);
19+
}
20+
21+
&:after {
1822
border-color: rgba(188, 193, 205, 0.5);
23+
opacity: 1;
1924
}
2025

2126
&:hover {
2227
color: #fff;
23-
.swim-button-wave {
24-
opacity: 1;
25-
}
26-
&:before {
28+
&:after {
2729
border-color: #2b65f4;
2830
}
2931
}
30-
31-
&-active {
32-
.swim-button-wave {
33-
background-color: #2b65f4;
34-
opacity: 0;
35-
transition-delay: 50ms;
36-
transition-timing-function: ease-in-out;
37-
transition-duration: 150ms;
38-
transition-property: opacity;
39-
}
40-
}
4132
}
4233
}
4334
}

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@
3030
"react": ">=16.8.0",
3131
"react-dom": ">=16.8.0"
3232
},
33-
"dependencies": {
34-
"@tweenjs/tween.js": "^18.6.4"
35-
},
3633
"devDependencies": {
3734
"@testing-library/jest-dom": "^5.15.1",
3835
"@testing-library/react": "^12.1.2",

src/index.tsx

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import TWEEN from '@tweenjs/tween.js';
2-
import React, { CSSProperties, ReactElement, useEffect, useRef } from 'react';
1+
import React, { CSSProperties, ReactElement } from 'react';
32

43
function clsx(...classnames: (string | undefined | boolean)[]) {
54
return classnames.filter((item) => !!item).join(' ');
@@ -17,58 +16,17 @@ function ReactSwimButton(props: ReactSwimButtonProps) {
1716
loading,
1817
duration = 250,
1918
} = props;
20-
const waveRef = useRef(null);
21-
function animate(time: number | undefined) {
22-
requestAnimationFrame(animate);
23-
TWEEN.update(time);
24-
}
25-
function updateProperty(coords: { width: number }) {
26-
if (waveRef.current) {
27-
// @ts-ignore
28-
waveRef.current.style.setProperty('width', `${coords.width}%`);
29-
}
30-
}
31-
function _onMouseEnter() {
32-
const coords = { width: 0 };
33-
const tween = new TWEEN.Tween(coords)
34-
.to({ width: 100 }, duration)
35-
.easing(TWEEN.Easing.Cubic.InOut) // Use an easing function to make the animation smooth.
36-
.onUpdate(updateProperty);
37-
// @ts-ignore
38-
waveRef.current.style.removeProperty('right');
39-
// @ts-ignore
40-
waveRef.current.style.setProperty('left', `0px`);
41-
tween.start();
42-
}
43-
function _onMouseLeave() {
44-
const coords = { width: 100 };
45-
const tween = new TWEEN.Tween(coords)
46-
.to({ width: 0 }, duration)
47-
.easing(TWEEN.Easing.Cubic.InOut) // Use an easing function to make the animation smooth.
48-
.onUpdate(updateProperty);
49-
// @ts-ignore
50-
waveRef.current.style.removeProperty('left');
51-
// @ts-ignore
52-
waveRef.current.style.setProperty('right', `0px`);
53-
tween.start();
54-
}
5519
function _onClick() {
5620
if (loading) return;
5721
onClick && onClick();
5822
}
59-
useEffect(() => {
60-
requestAnimationFrame(animate);
61-
}, []);
6223
return (
6324
<div
6425
className={clsx('swim-button-box', className, active && `swim-button-box-active`)}
65-
onMouseEnter={_onMouseEnter}
66-
onMouseLeave={_onMouseLeave}
6726
onClick={_onClick}
6827
// @ts-ignore
69-
style={{ ...style, [`--primary-color`]: primary }}
28+
style={{ ...style, [`--primary-color`]: primary, [`--swim-duration`]: `${duration}ms` }}
7029
>
71-
<div ref={waveRef} className={clsx('swim-button-wave')} style={{ left: 0 }} />
7230
<span className={clsx('swim-button')}>
7331
{icon && !loading && <span className={clsx('swim-button-icon')}>{icon}</span>}
7432
{loading && (

src/style/index.less

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,52 +10,79 @@
1010
background-color: transparent;
1111
border-width: 0;
1212
cursor: pointer;
13+
1314
&-box {
1415
--primary-color: #2b65f4;
1516
position: relative;
1617
display: inline-block;
1718
box-sizing: border-box;
1819
height: 48px;
1920
padding: 0 24px;
20-
overflow: hidden;
2121
color: #858b9b;
2222
font-size: 14px;
2323
line-height: 20px;
2424
text-align: center;
25+
vertical-align: top;
2526
background-color: #fff;
2627
border-radius: 4px;
2728
box-shadow: 0 3px 12px rgba(188, 193, 205, 0.3);
2829
cursor: pointer;
2930
transition-timing-function: ease;
30-
transition-duration: 0.25s;
31+
transition-duration: var(--swim-duration);
3132
transition-property: color;
3233
user-select: none;
34+
35+
&::before {
36+
position: absolute;
37+
top: 0;
38+
left: 0;
39+
width: 100%;
40+
height: 100%;
41+
background-color: var(--primary-color);
42+
border-radius: 4px;
43+
transform: rotateY(90deg);
44+
transform-origin: right;
45+
opacity: 1;
46+
transition: transform var(--swim-duration) ease-in-out;
47+
content: '';
48+
}
49+
50+
&::after {
51+
position: absolute;
52+
top: 0;
53+
left: 0;
54+
box-sizing: border-box;
55+
width: 100%;
56+
height: 100%;
57+
border: 1px solid var(--primary-color);
58+
border-radius: 4px;
59+
opacity: 0;
60+
transition-delay: var(--swim-duration);
61+
content: '';
62+
}
63+
64+
&:hover::before {
65+
border-radius: 4px;
66+
transform: rotateY(0deg);
67+
transform-origin: left;
68+
}
69+
3370
&:hover {
3471
color: #fff;
3572
}
3673

3774
&.swim-button-box-active {
3875
color: #fff;
3976
background-color: var(--primary-color);
40-
&:before {
41-
position: absolute;
42-
top: 0;
43-
right: 0;
44-
bottom: 0;
45-
left: 0;
46-
z-index: 2;
47-
border-color: var(--primary-color);
48-
border-style: solid;
49-
border-width: 1px;
50-
border-radius: 4px;
51-
content: '';
77+
&::before {
78+
background-color: #fff;
5279
}
5380
&:hover {
5481
color: var(--primary-color);
55-
}
56-
57-
.swim-button-wave {
58-
background-color: #fff;
82+
&::after {
83+
opacity: 1;
84+
transition-delay: 0s;
85+
}
5986
}
6087
}
6188
}
@@ -67,14 +94,6 @@
6794
line-height: 100%;
6895
}
6996

70-
&-wave {
71-
position: absolute;
72-
top: 0;
73-
height: 100%;
74-
background-color: var(--primary-color);
75-
border-radius: 4px;
76-
}
77-
7897
&-loading {
7998
display: inline-block;
8099
color: inherit;

0 commit comments

Comments
 (0)