Skip to content

Commit 4362c5a

Browse files
committed
feature: large org changes to publish npm package
1 parent a61110b commit 4362c5a

19 files changed

+182
-320
lines changed

.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"presets": ["@babel/preset-env", "@babel/preset-react"]
2+
"presets": ["@babel/preset-env", ["@babel/preset-react", {"runtime": "automatic"}], "@babel/preset-typescript"]
33
}

.codesandbox/workspace.json

Lines changed: 0 additions & 20 deletions
This file was deleted.

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
node_modules
2+
23
public
4+
35
build
6+
47
.vscode
8+
59
.codesandbox
10+
611
package-lock.json
12+
13+
dist

src/__tests__/Fallback.test.tsx renamed to __tests__/Fallback.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Fallback from "../components/Fallback";
1+
import Fallback from "../src";
22
import { render, screen } from "@testing-library/react";
33

44
it("does not render a skeleton if data has been fetched", () => {
@@ -12,7 +12,7 @@ it("does not render a skeleton if data has been fetched", () => {
1212
});
1313

1414
it.each(['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'span', 'img'])
15-
("renders a skeleton if data has not yet been fetched", (t) => {
15+
("renders a skeleton if data has not yet been fetched for a %s element", (t) => {
1616
render(
1717
<Fallback isLoading fallbackOnStaticContent>
1818
<p>Lorem ipsum</p>

examples/index.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// <reference types="react" />
2+
interface FallbackProps {
3+
isLoading: boolean;
4+
children: JSX.Element | JSX.Element[];
5+
fallbackOnStaticContent?: boolean;
6+
}
7+
export default function Fallback({ isLoading, children, fallbackOnStaticContent }: FallbackProps): JSX.Element;
8+
export {};

examples/index.tsx

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
Object.defineProperty(exports, '__esModule', { value: true });
2+
3+
var jsxRuntime = require('react/jsx-runtime');
4+
var React = require('react');
5+
6+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
7+
8+
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
9+
10+
var tags = ["P", "H1", "H2", "H3", "H4", "H5", "H6", "SPAN", "IMG"];
11+
function assignFallbackAttributes(el) {
12+
var styles = "";
13+
el.textContent = el.textContent || "_";
14+
var css = window.getComputedStyle(el);
15+
for (var i = 0; i < css.length; i++) {
16+
styles += "background-color:rgba(0, 0, 0, 0.11);animation: skeleton 1s linear infinite alternate both;".concat(css[i], ":").concat(css.getPropertyValue("" + css[i]), ";");
17+
}
18+
return styles;
19+
}
20+
function useFallback(_a) {
21+
var isLoading = _a.isLoading, fallbackOnStaticContent = _a.fallbackOnStaticContent;
22+
var ref = React.useRef();
23+
var withFallback = function (tree) {
24+
var Fallback = React__default["default"].forwardRef(function (_, ref) {
25+
return React__default["default"].cloneElement(tree, { ref: ref });
26+
});
27+
if (isLoading) {
28+
return (jsxRuntime.jsx(Fallback, { ref: function (el) {
29+
var _a;
30+
ref.current = el;
31+
(_a = ref.current) === null || _a === void 0 ? void 0 : _a.querySelectorAll("*").forEach(function (el) {
32+
var _a;
33+
if (tags.includes(el.tagName) || ((_a = el.firstChild) === null || _a === void 0 ? void 0 : _a.nodeValue)) {
34+
var skeleton = document.createElement("div");
35+
if (fallbackOnStaticContent || !el.textContent) {
36+
skeleton.style.cssText = assignFallbackAttributes(el);
37+
el.replaceWith(skeleton);
38+
}
39+
}
40+
});
41+
} }));
42+
}
43+
else {
44+
return tree;
45+
}
46+
};
47+
return { withFallback: withFallback };
48+
}
49+
50+
function Fallback(_a) {
51+
var isLoading = _a.isLoading, children = _a.children, fallbackOnStaticContent = _a.fallbackOnStaticContent;
52+
var withFallback = useFallback({ isLoading: isLoading, fallbackOnStaticContent: fallbackOnStaticContent }).withFallback;
53+
return withFallback(jsxRuntime.jsx("div", { children: children }));
54+
}
55+
56+
exports["default"] = Fallback;
57+
//# sourceMappingURL=index.tsx.map

examples/index.tsx.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/useFallback.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference types="react" />
2+
interface UseFallbackProps {
3+
isLoading: boolean;
4+
fallbackOnStaticContent?: boolean;
5+
}
6+
export default function useFallback({ isLoading, fallbackOnStaticContent }: UseFallbackProps): {
7+
withFallback: (tree: JSX.Element) => JSX.Element;
8+
};
9+
export {};

jest.config.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// For a detailed explanation regarding each configuration property, visit:
2+
// https://jestjs.io/docs/en/configuration.html
3+
4+
module.exports = {
5+
moduleFileExtensions: [
6+
"js",
7+
"ts",
8+
"jsx",
9+
"tsx"
10+
],
11+
testEnvironment: 'node',
12+
testMatch: ['**/*.test.[jt]s?(x)'],
13+
testEnvironment: 'jest-environment-jsdom',
14+
testPathIgnorePatterns: ['/node_modules/'],
15+
};

package.json

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "react-fallback",
3-
"version": "1.0.0",
2+
"name": "@steviecs/react-fallback",
3+
"version": "1.0.1",
44
"author": "steviecs",
55
"description": "A POC component library for rendering adaptive skeletons that adjust to the shape of your UI's layout while data is being fetched",
66
"keywords": [
@@ -12,9 +12,10 @@
1212
"fallback"
1313
],
1414
"license": "MIT",
15-
"main": "src/index.tsx",
16-
"dependencies": {
17-
"react": "17.0.2"
15+
"main": "dist/index.tsx",
16+
"peerDependencies": {
17+
"react": "^17.0.2",
18+
"react-dom": "^17.0.2"
1819
},
1920
"devDependencies": {
2021
"@emotion/react": "11.7.1",
@@ -25,16 +26,19 @@
2526
"@types/jest": "^27.5.1",
2627
"@types/react": "^17.0.37",
2728
"@types/react-dom": "17.0.0",
29+
"babel-core": "^6.26.3",
30+
"babel-runtime": "^6.26.0",
31+
"jest": "^27.5.1",
2832
"mui": "0.0.1",
29-
"react-dom": "17.0.2",
3033
"react-scripts": "^5.0.1",
34+
"rollup": "^2.75.3",
35+
"rollup-plugin-typescript2": "^0.31.2",
3136
"typescript": "^4.7.2"
3237
},
3338
"scripts": {
34-
"start": "react-scripts start",
35-
"build": "react-scripts build",
36-
"test": "react-scripts test",
37-
"eject": "react-scripts eject"
39+
"build": "rollup -c",
40+
"start": "rollup -c -w",
41+
"test": "jest"
3842
},
3943
"browserslist": {
4044
"production": [
@@ -47,5 +51,8 @@
4751
"last 1 firefox version",
4852
"last 1 safari version"
4953
]
50-
}
54+
},
55+
"files": [
56+
"dist"
57+
]
5158
}

0 commit comments

Comments
 (0)