Skip to content

Commit 30c6d74

Browse files
committed
Add SSR support
1 parent 90f92a5 commit 30c6d74

File tree

9 files changed

+81
-43
lines changed

9 files changed

+81
-43
lines changed

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
**
22
!lib/**/*
33
!index.js
4+
!style.css

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ Modals, tabs, side menus, popovers, popups, dropdowns, notifications; These are
2626

2727
This library is currently only available for the web, and since it uses Web Animations API it's recommended to load a [polyfill](https://github.com/web-animations/web-animations-js) for comparability reasons with older browsers. This library is also very new and may have some missing features. Please, don't hesitate and open a ticket in the [issues](https://github.com/DAB0mB/react-layers-stack/issues) section if you encounter one, and I will address it as soon as I can.
2828

29+
### SSR
30+
31+
The library does work with SSR frameworks e.g. Gatsby or Next.JS, just make sure to import `react-layers-stack/style.css` file additionally.
32+
2933
### Examples
3034

3135
This repo contains example React apps under the [examples](/examples) dir. To run an example app, first make sure it's initialized by running `yarn` and then you can run it using `yarn start`.
@@ -47,6 +51,7 @@ Below is a detailed documentation regards the library's API, but I also encourag
4751
This is the main stack container that will define the boundaries for our layers and provide them with the necessary context. A React element of this component type HAS to be created if we want to use react-layers-stack and its React hooks. Initialization is as simple as the following:
4852

4953
```js
54+
import 'react-layers-stack/style.css';
5055
import { Stack } from 'react-layers-stack';
5156

5257
const MyApp = () => {
@@ -59,6 +64,7 @@ const MyApp = () => {
5964
By providing a render function as children, you can also define a fixed layout that will keep displaying on top of layers, e.g. a header of a navigation bar:
6065

6166
```js
67+
import 'react-layers-stack/style.css';
6268
import { Stack } from 'react-layers-stack';
6369

6470
const MyApp = () => {
@@ -78,6 +84,7 @@ const MyApp = () => {
7884
Internally, the render function is used as the body of a separate component, which means that react-layers-stack hooks can also be used:
7985

8086
```js
87+
import 'react-layers-stack/style.css';
8188
import { Stack, usePopLayer } from 'react-layers-stack';
8289

8390
const MyApp = () => {
@@ -106,6 +113,7 @@ const MyApp = () => {
106113
You can also initialize a Stack with a predefined layers state like the following:
107114

108115
```js
116+
import 'react-layers-stack/style.css';
109117
import { Stack, createLayer } from 'react-layers-stack';
110118

111119
const MyApp = () => {
@@ -145,6 +153,7 @@ This hook will initialize a callback that will push a new layer on top of the st
145153
Here's an example:
146154

147155
```js
156+
import 'react-layers-stack/style.css';
148157
import { usePushLayer } from 'react-layers-stack';
149158

150159
const usePushMyLayer = () => {
@@ -161,7 +170,7 @@ const usePushMyLayer = () => {
161170
timing: {
162171
duration: 500,
163172
},
164-
overlay: {
173+
mask: {
165174
background: '#000',
166175
},
167176
});

examples/hamburger/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'react-layers-stack/style.css';
12
import './index.css';
23
import React from 'react';
34
import ReactDOM from 'react-dom';

examples/widgets/src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'react-layers-stack/style.css';
12
import './index.css';
23
import React from 'react';
34
import ReactDOM from 'react-dom';

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-layers-stack",
33
"description": "Push and pop React view layers with transitions",
4-
"version": "0.1.5",
4+
"version": "0.2.0",
55
"license": "MIT",
66
"author": "DAB0mB",
77
"repository": {
@@ -34,6 +34,7 @@
3434
"eslint-plugin-react": "^7.18.3",
3535
"husky": "^4.2.1",
3636
"lint-staged": "^10.0.7",
37+
"raw-loader": "^4.0.2",
3738
"typescript": "^4.0.3",
3839
"webpack": "^4.41.5",
3940
"webpack-cli": "^3.3.10",

src/index.js

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,13 @@ export * from './contexts/LayerContext';
22
export * from './contexts/StackContext';
33
export * from './components/Layer';
44

5-
import React, { useMemo, useState } from 'react';
5+
import React, { useState } from 'react';
66

77
import { StackProvider } from './contexts/StackContext';
88

99
export const Stack = ({ layersState, style, className, children }) => {
1010
const [layers, setLayers] = layersState ?? useState([]);
1111

12-
useMemo(() => {
13-
let styleEl = document.getElementById('rls-style');
14-
15-
if (styleEl) {
16-
return;
17-
}
18-
19-
styleEl = document.createElement('style');
20-
styleEl.id = 'rls-style';
21-
styleEl.innerHTML = `
22-
.rls-stack {
23-
position: relative;
24-
width: 100%;
25-
height: 100%;
26-
}
27-
28-
.rls-layer {
29-
top: 0;
30-
right: 0;
31-
bottom: 0;
32-
left: 0;
33-
position: absolute;
34-
}
35-
36-
.rls-mask {
37-
top: 0;
38-
right: 0;
39-
bottom: 0;
40-
left: 0;
41-
position: absolute;
42-
}
43-
`;
44-
45-
document.head.append(styleEl);
46-
}, []);
47-
4812
return (
4913
<StackProvider layersState={[layers, setLayers]}>
5014
{stack => (

style.css

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.rls-stack {
2+
position: relative;
3+
width: 100%;
4+
height: 100%;
5+
}
6+
7+
.rls-layer {
8+
top: 0;
9+
right: 0;
10+
bottom: 0;
11+
left: 0;
12+
position: absolute;
13+
}
14+
15+
.rls-mask {
16+
top: 0;
17+
right: 0;
18+
bottom: 0;
19+
left: 0;
20+
position: absolute;
21+
}

webpack.config.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module.exports = {
55
mode: 'production',
66
devtool: 'sourcemap',
77
entry: {
8-
index: path.resolve(__dirname, 'src/index'),
8+
index: path.resolve(__dirname, 'src/index.js'),
99
},
1010
output: {
1111
path: path.resolve(__dirname, 'lib'),
@@ -24,8 +24,12 @@ module.exports = {
2424
},
2525
{
2626
test: /\.json$/,
27-
loader: 'json-loader',
28-
}
27+
use: 'json-loader',
28+
},
29+
{
30+
test: /\.css$/i,
31+
use: ['raw-loader']
32+
},
2933
]
3034
},
3135
externals: [

yarn.lock

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,7 @@
920920
resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"
921921
integrity sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ==
922922

923-
"@types/json-schema@^7.0.5":
923+
"@types/json-schema@^7.0.5", "@types/json-schema@^7.0.6":
924924
version "7.0.6"
925925
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.6.tgz#f4c7ec43e81b319a9815115031709f26987891f0"
926926
integrity sha512-3c+yGKvVP5Y9TYBEibGNR+kLtijnj7mYrXRg+WpFb2X9xm04g/DXYkfg4hmzJQosc9snFNUPkbYIhu+KAm6jJw==
@@ -1141,6 +1141,16 @@ ajv@^6.1.0, ajv@^6.10.0, ajv@^6.10.2, ajv@^6.12.4:
11411141
json-schema-traverse "^0.4.1"
11421142
uri-js "^4.2.2"
11431143

1144+
ajv@^6.12.5:
1145+
version "6.12.6"
1146+
resolved "https://registry.yarnpkg.com/ajv/-/ajv-6.12.6.tgz#baf5a62e802b07d977034586f8c3baf5adf26df4"
1147+
integrity sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==
1148+
dependencies:
1149+
fast-deep-equal "^3.1.1"
1150+
fast-json-stable-stringify "^2.0.0"
1151+
json-schema-traverse "^0.4.1"
1152+
uri-js "^4.2.2"
1153+
11441154
ansi-colors@^4.1.1:
11451155
version "4.1.1"
11461156
resolved "https://registry.yarnpkg.com/ansi-colors/-/ansi-colors-4.1.1.tgz#cbb9ae256bf750af1eab344f229aa27fe94ba348"
@@ -3290,6 +3300,15 @@ loader-utils@^1.2.3, loader-utils@^1.4.0:
32903300
emojis-list "^3.0.0"
32913301
json5 "^1.0.1"
32923302

3303+
loader-utils@^2.0.0:
3304+
version "2.0.0"
3305+
resolved "https://registry.yarnpkg.com/loader-utils/-/loader-utils-2.0.0.tgz#e4cace5b816d425a166b5f097e10cd12b36064b0"
3306+
integrity sha512-rP4F0h2RaWSvPEkD7BLDFQnvSf+nK+wr3ESUjNTyAGobqrijmW92zc+SO6d4p4B1wh7+B/Jg1mkQe5NYUEHtHQ==
3307+
dependencies:
3308+
big.js "^5.2.2"
3309+
emojis-list "^3.0.0"
3310+
json5 "^2.1.2"
3311+
32933312
locate-path@^3.0.0:
32943313
version "3.0.0"
32953314
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-3.0.0.tgz#dbec3b3ab759758071b58fe59fc41871af21400e"
@@ -4043,6 +4062,14 @@ randomfill@^1.0.3:
40434062
randombytes "^2.0.5"
40444063
safe-buffer "^5.1.0"
40454064

4065+
raw-loader@^4.0.2:
4066+
version "4.0.2"
4067+
resolved "https://registry.yarnpkg.com/raw-loader/-/raw-loader-4.0.2.tgz#1aac6b7d1ad1501e66efdac1522c73e59a584eb6"
4068+
integrity sha512-ZnScIV3ag9A4wPX/ZayxL/jZH+euYb6FcUinPcgiQW0+UBtEv0O6Q3lGd3cqJ+GHH+rksEv3Pj99oxJ3u3VIKA==
4069+
dependencies:
4070+
loader-utils "^2.0.0"
4071+
schema-utils "^3.0.0"
4072+
40464073
react-is@^16.8.1:
40474074
version "16.13.1"
40484075
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4"
@@ -4311,6 +4338,15 @@ schema-utils@^2.6.5:
43114338
ajv "^6.12.4"
43124339
ajv-keywords "^3.5.2"
43134340

4341+
schema-utils@^3.0.0:
4342+
version "3.0.0"
4343+
resolved "https://registry.yarnpkg.com/schema-utils/-/schema-utils-3.0.0.tgz#67502f6aa2b66a2d4032b4279a2944978a0913ef"
4344+
integrity sha512-6D82/xSzO094ajanoOSbe4YvXWMfn2A//8Y1+MUqFAJul5Bs+yn36xbK9OtNDcRVSBJ9jjeoXftM6CfztsjOAA==
4345+
dependencies:
4346+
"@types/json-schema" "^7.0.6"
4347+
ajv "^6.12.5"
4348+
ajv-keywords "^3.5.2"
4349+
43144350
semver-compare@^1.0.0:
43154351
version "1.0.0"
43164352
resolved "https://registry.yarnpkg.com/semver-compare/-/semver-compare-1.0.0.tgz#0dee216a1c941ab37e9efb1788f6afc5ff5537fc"

0 commit comments

Comments
 (0)