Skip to content

Commit a404b5f

Browse files
committed
➕ Added jotai for state management
🐛 Added scope to prevent atomSharing between multiple custoplayers
1 parent a1a7263 commit a404b5f

File tree

7 files changed

+40
-15
lines changed

7 files changed

+40
-15
lines changed

.prettierrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"semi": true,
33
"tabWidth": 2,
4-
"printWidth": 100,
4+
"printWidth": 80,
55
"singleQuote": true,
66
"trailingComma": "all",
77
"jsxSingleQuote": true,

packages/custoplayer/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"build-storybook": "build-storybook"
1111
},
1212
"dependencies": {
13+
"jotai": "^1.13.1",
1314
"react": "17.0.2",
1415
"react-dom": "17.0.2",
1516
"styled-components": "5.3.5"
@@ -39,7 +40,8 @@
3940
"peerDependencies": {
4041
"react": "16.8.0 || 17.x",
4142
"react-dom": "16.8.0 || 17.x",
42-
"styled-components": "5.0.0"
43+
"styled-components": "5.0.0",
44+
"jotai": "1.13.1"
4345
},
4446
"files": [
4547
"dist"

packages/custoplayer/src/App.tsx

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import MyButton from './lib/MyButton';
44
function App() {
55
return (
66
<Wrapper>
7-
<MyButton>Click me</MyButton>
8-
<MyButton>Click me</MyButton>
9-
<MyButton>Click me</MyButton> <MyButton>Click me</MyButton>
7+
<MyButton></MyButton>
108
</Wrapper>
119
);
1210
}

packages/custoplayer/src/lib/MyButton.stories.tsx

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import { ComponentMeta, ComponentStoryObj } from '@storybook/react';
22
import MyButton from './MyButton';
33

44
const meta: ComponentMeta<typeof MyButton> = {
5-
title: 'Design System/MyButton',
6-
component: MyButton,
5+
title: 'Design System/MyButton',
6+
component: MyButton,
77
};
88
export default meta;
99

1010
export const Primary: ComponentStoryObj<typeof MyButton> = {
11-
args: {
12-
disabled: false,
13-
children: 'Hello',
14-
},
11+
args: {
12+
disabled: false,
13+
children: 'Hello',
14+
},
1515
};

packages/custoplayer/src/lib/MyButton.tsx

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
import styled from 'styled-components';
2+
import { Provider, atom, useAtom } from 'jotai';
3+
import { useMemo } from 'react';
24

3-
const MyButton = styled.button`
5+
const countAtom = atom(0);
6+
7+
function MyButton() {
8+
/*
9+
Scope is required to prevent two custoplayer's
10+
from sharing the same atoms
11+
*/
12+
const myScope = useMemo(() => {
13+
return Symbol();
14+
}, []);
15+
const [count, setCount] = useAtom(countAtom, myScope);
16+
return (
17+
<Provider scope={myScope}>
18+
<StyledButton onClick={() => setCount((prev) => prev + 1)}>
19+
{count}
20+
</StyledButton>
21+
</Provider>
22+
);
23+
}
24+
const StyledButton = styled.button`
425
border: none;
526
border-radius: 0.5rem;
627
background-color: green;

sites/my-site/src/App.tsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ import { MyButton } from 'custoplayer';
33
import './App.css';
44

55
function App() {
6-
const [count, setCount] = useState(0);
7-
86
return (
97
<div className='App'>
108
<p>
11-
<MyButton onClick={() => setCount((count) => count + 1)}>Click here!</MyButton>
9+
<MyButton>Click here!</MyButton>
10+
<MyButton>Click here!</MyButton>
1211
</p>
1312
</div>
1413
);

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -7029,6 +7029,11 @@ jest-worker@^27.4.5:
70297029
merge-stream "^2.0.0"
70307030
supports-color "^8.0.0"
70317031

7032+
jotai@^1.13.1:
7033+
version "1.13.1"
7034+
resolved "https://registry.yarnpkg.com/jotai/-/jotai-1.13.1.tgz#20cc46454cbb39096b12fddfa635b873b3668236"
7035+
integrity sha512-RUmH1S4vLsG3V6fbGlKzGJnLrDcC/HNb5gH2AeA9DzuJknoVxSGvvg8OBB7lke+gDc4oXmdVsaKn/xDUhWZ0vw==
7036+
70327037
js-string-escape@^1.0.1:
70337038
version "1.0.1"
70347039
resolved "https://registry.yarnpkg.com/js-string-escape/-/js-string-escape-1.0.1.tgz#e2625badbc0d67c7533e9edc1068c587ae4137ef"

0 commit comments

Comments
 (0)