Skip to content

Commit fa84de9

Browse files
committed
Add portalInto prop to provider #6; Add github action #2
1 parent 610620e commit fa84de9

File tree

6 files changed

+78
-28
lines changed

6 files changed

+78
-28
lines changed

.github/workflows/release.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: 'Build library, upload dist files'
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- master
8+
paths:
9+
- 'package.json'
10+
11+
jobs:
12+
compile:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v2
16+
- uses: actions/setup-node@v2
17+
with:
18+
node-version: 16
19+
cache: 'yarn'
20+
- run: yarn install --frozen-lockfile
21+
- id: set_var
22+
run: |
23+
content=`cat ./package.json`
24+
# the following lines are only required for multi line json
25+
content="${content//'%'/'%25'}"
26+
content="${content//$'\n'/'%0A'}"
27+
content="${content//$'\r'/'%0D'}"
28+
# end of optional handling for multi line json
29+
echo "::set-output name=packageJson::$content"
30+
- run: yarn run build
31+
- name: Release
32+
uses: softprops/action-gh-release@v1
33+
with:
34+
tag_name: "v${{fromJson(steps.set_var.outputs.packageJson).version}}"
35+
generate_release_notes: true
36+
files: |
37+
dist/bundle.css
38+
dist/bundle.min.css
39+
dist/react-local-toast.cjs.development.js
40+
dist/react-local-toast.cjs.production.js
41+
dist/react-local-toast.esm.development.js
42+
dist/react-local-toast.esm.production.js
43+
dist/react-local-toast.umd.development.js
44+
dist/react-local-toast.umd.production.js

.size-snapshot.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
{
22
"react-local-toast.cjs.development.js": {
3-
"bundled": 19425,
4-
"minified": 9713,
5-
"gzipped": 2971
3+
"bundled": 19556,
4+
"minified": 9736,
5+
"gzipped": 2987
66
},
77
"react-local-toast.cjs.production.js": {
8-
"bundled": 19425,
9-
"minified": 9713,
10-
"gzipped": 2971
8+
"bundled": 19556,
9+
"minified": 9736,
10+
"gzipped": 2987
1111
},
1212
"react-local-toast.esm.js": {
13-
"bundled": 18227,
14-
"minified": 8737,
15-
"gzipped": 2827,
13+
"bundled": 18378,
14+
"minified": 8777,
15+
"gzipped": 2838,
1616
"treeshaked": {
1717
"rollup": {
1818
"code": 225,
1919
"import_statements": 225
2020
},
2121
"webpack": {
22-
"code": 8596
22+
"code": 8638
2323
}
2424
}
2525
},
2626
"react-local-toast.umd.development.js": {
27-
"bundled": 108832,
28-
"minified": 34254,
29-
"gzipped": 11214
27+
"bundled": 108975,
28+
"minified": 34290,
29+
"gzipped": 11231
3030
},
3131
"react-local-toast.umd.production.js": {
32-
"bundled": 58304,
33-
"minified": 21259,
34-
"gzipped": 7119
32+
"bundled": 58447,
33+
"minified": 21295,
34+
"gzipped": 7140
3535
},
3636
"internals.js": {
3737
"bundled": 10559,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-local-toast",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "Local toast helps you to provide feedback related to particular components on page",
55
"main": "dist/index.js",
66
"module": "dist/react-local-toast.esm.js",

src/provider.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface LocalToastProviderProps<T> {
1010
Component?: ToastComponentType<T>;
1111
animationDuration?: number;
1212
defaultPlacement?: ToastPlacement;
13+
portalInto?: HTMLElement,
1314
}
1415

1516
export type LocalToastProviderType<T> = (props: LocalToastProviderProps<T>) => JSX.Element;
@@ -25,6 +26,7 @@ export const createProvider = <T,>(
2526
Component = ToastComponent,
2627
animationDuration = DEFAULT_ANIMATION_DURATION,
2728
defaultPlacement = DEFAULT_PLACEMENT,
29+
portalInto = document.body,
2830
}: LocalToastProviderProps<T>) => {
2931
const [refs, setRefs] = React.useState<LocalToastContextType<T>['refs']>({});
3032
const [q, setQ] = React.useState<LocalToastContextType<T>['q']>([]);
@@ -122,7 +124,7 @@ export const createProvider = <T,>(
122124
}}
123125
>
124126
{children}
125-
<Viewport />
127+
<Viewport portalInto={portalInto} />
126128
</Context.Provider>
127129
);
128130
};

src/styles.scss

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
pointer-events: none;
33
margin-right: 12px;
44
font-size: 24px;
5+
min-width: 24px;
6+
min-height: 24px;
57
}
68

79
$colors: (
@@ -31,7 +33,7 @@ $colors: (
3133

3234

3335
.react-local-toast-text {
34-
line-height: 1;
36+
line-height: 1.5;
3537
flex-grow: 1;
3638
}
3739

@@ -44,7 +46,7 @@ $colors: (
4446
box-shadow: 0 3px 6px -4px #0000001f, 0 6px 16px #00000014, 0 9px 28px 8px #0000000d;
4547
border-radius: 3px;
4648
min-width: 150px;
47-
max-width: min(300px, calc(80vw - 8px));
49+
max-width: min(350px, calc(80vw - 8px));
4850
min-height: 30px;
4951
display: flex;
5052
justify-content: center;

src/viewport.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ interface ToastInfo<T> {
1717
data: T;
1818
}
1919

20+
interface ViewportProps {
21+
portalInto?: HTMLElement,
22+
}
23+
2024
// Source: https://stackoverflow.com/questions/19014250/rerender-view-on-browser-resize-with-react
2125
const useWindowSize = () => {
2226
const [size, setSize] = React.useState([0, 0]);
@@ -32,7 +36,7 @@ const useWindowSize = () => {
3236
};
3337

3438
export const createViewport = <T,>(context: Context<LocalToastContextType<T>>) => {
35-
return () => {
39+
return ({portalInto}: ViewportProps) => {
3640
const renderToast = (toast: ToastInfo<T>) => {
3741
const removeMe = () => {
3842
setToasts((t) => t.filter((tst) => tst.id !== toast.id));
@@ -125,17 +129,14 @@ export const createViewport = <T,>(context: Context<LocalToastContextType<T>>) =
125129
};
126130

127131
// Document is unavailable in Next.js SSR, so postpone actual rendering of portal
128-
const ref = React.useRef<HTMLElement>();
129132
const [mounted, setMounted] = React.useState(false);
130-
131-
// We don't really need window size, but we need to re-render once window size changes
132-
const [width, height] = useWindowSize();
133-
134133
React.useEffect(() => {
135-
ref.current = document.body;
136134
setMounted(true);
137135
}, []);
138136

137+
// We don't really need window size, but we need to re-render once window size changes
138+
const [width, height] = useWindowSize();
139+
139140
const { q, setQ, Component, animationDuration } = React.useContext(context);
140141
const [toasts, setToasts] = React.useState<ToastInfo<T>[]>([]);
141142

@@ -216,11 +217,12 @@ export const createViewport = <T,>(context: Context<LocalToastContextType<T>>) =
216217
});
217218

218219
if (!mounted) return null;
220+
219221
return ReactDOM.createPortal(
220222
<>
221223
<TransitionGroup component={null}>{toasts.map(renderToast)}</TransitionGroup>
222224
</>,
223-
ref.current!
225+
portalInto || document.body
224226
);
225227
};
226228
};

0 commit comments

Comments
 (0)