Skip to content

Commit 9634ffa

Browse files
authored
feat: publish es modules (#554)
1 parent 83d9174 commit 9634ffa

File tree

6 files changed

+79
-42
lines changed

6 files changed

+79
-42
lines changed

babel.config.json

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
{
2-
"presets": [
3-
[
4-
"@babel/env",
5-
{
6-
"useBuiltIns": "usage",
7-
"corejs": 3
8-
}
9-
],
10-
"@babel/preset-react"
11-
]
2+
"presets": ["@babel/preset-react"],
3+
"env": {
4+
"commonjs": {
5+
"plugins": ["add-module-exports"],
6+
"presets": [
7+
[
8+
"@babel/preset-env",
9+
{
10+
"corejs": 3,
11+
"useBuiltIns": "usage",
12+
"modules": "commonjs"
13+
}
14+
]
15+
]
16+
},
17+
"es": {
18+
"presets": [
19+
[
20+
"@babel/preset-env",
21+
{
22+
"corejs": 3,
23+
"useBuiltIns": "usage",
24+
"modules": false
25+
}
26+
]
27+
]
28+
}
29+
}
1230
}

package-lock.json

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

package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
"name": "react-stickynode",
33
"version": "3.1.1",
44
"description": "A performant and comprehensive React sticky component",
5-
"main": "dist/Sticky.js",
5+
"main": "./dist/cjs/Sticky.js",
6+
"module": "./dist/es/Sticky.js",
67
"scripts": {
7-
"build": "babel src --out-dir dist",
8+
"build": "npm run clean && npm run build:commonjs && npm run build:es",
9+
"build:commonjs": "babel --env-name commonjs src -d dist/cjs",
10+
"build:es": "babel --env-name es src -d dist/es",
811
"build:babel": "babel tests/functional --out-dir tests/functional/dist/",
912
"build:copy": "cp tests/functional/*.html tests/functional/dist/",
1013
"build:css": "atomizer -o tests/functional/dist/atomic.css ./tests/functional/dist/*-functional.js",
1114
"build:webpack": "webpack",
15+
"clean": "rm -rf dist",
1216
"format": "prettier --write .",
1317
"format:check": "prettier --check .",
1418
"func": "wdio",
@@ -59,6 +63,7 @@
5963
"@wdio/static-server-service": "^7.7.3",
6064
"atomizer": "^3.9.1",
6165
"babel-jest": "^27.0.2",
66+
"babel-plugin-add-module-exports": "^1.0.4",
6267
"chromedriver": "^92.0.0",
6368
"eslint": "^7.0.0",
6469
"eslint-plugin-react": "^7.19.0",
@@ -88,7 +93,7 @@
8893
"last 2 versions",
8994
"ie >= 11",
9095
"iOS >= 12",
91-
"Android >= 6"
96+
"Android >= 11"
9297
],
9398
"prettier": {
9499
"singleQuote": true,

src/Sticky.jsx

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ const STATUS_ORIGINAL = 0; // The default status, locating at the original posit
1717
const STATUS_RELEASED = 1; // The released status, locating at somewhere on document but not default one.
1818
const STATUS_FIXED = 2; // The sticky status, locating fixed to the top or the bottom of screen.
1919

20-
var TRANSFORM_PROP = 'transform';
20+
let TRANSFORM_PROP = 'transform';
2121

2222
// global variable for all instances
23-
var doc;
24-
var docBody;
25-
var docEl;
26-
var canEnableTransforms = true; // Use transform by default, so no Sticky on lower-end browser when no Modernizr
27-
var M;
28-
var scrollDelta = 0;
29-
var win;
30-
var winHeight = -1;
23+
let doc;
24+
let docBody;
25+
let docEl;
26+
let canEnableTransforms = true; // Use transform by default, so no Sticky on lower-end browser when no Modernizr
27+
let M;
28+
let scrollDelta = 0;
29+
let win;
30+
let winHeight = -1;
3131

3232
class Sticky extends Component {
3333
constructor(props, context) {
@@ -81,13 +81,13 @@ class Sticky extends Component {
8181
if (!target) {
8282
return -1;
8383
}
84-
var rect = target.getBoundingClientRect();
84+
const rect = target.getBoundingClientRect();
8585
return this.scrollTop + rect.bottom;
8686
}
8787

8888
getBottomBoundary(bottomBoundary) {
8989
// a bottomBoundary can be provided to avoid reading from the props
90-
var boundary = bottomBoundary || this.props.bottomBoundary;
90+
let boundary = bottomBoundary || this.props.bottomBoundary;
9191

9292
// TODO, bottomBoundary was an object, depricate it later.
9393
if (typeof boundary === 'object') {
@@ -135,18 +135,18 @@ class Sticky extends Component {
135135
return;
136136
}
137137

138-
var outerRect = this.outerElement.getBoundingClientRect();
139-
var innerRect = this.innerElement.getBoundingClientRect();
138+
const outerRect = this.outerElement.getBoundingClientRect();
139+
const innerRect = this.innerElement.getBoundingClientRect();
140140

141-
var width = outerRect.width || outerRect.right - outerRect.left;
142-
var height = innerRect.height || innerRect.bottom - innerRect.top;
143-
var outerY = outerRect.top + this.scrollTop;
141+
const width = outerRect.width || outerRect.right - outerRect.left;
142+
const height = innerRect.height || innerRect.bottom - innerRect.top;
143+
const outerY = outerRect.top + this.scrollTop;
144144

145145
this.setState({
146146
top: this.getTopPosition(options.top),
147147
bottom: Math.min(this.state.top + height, winHeight),
148-
width: width,
149-
height: height,
148+
width,
149+
height,
150150
x: outerRect.left,
151151
y: outerY,
152152
bottomBoundary: this.getBottomBoundary(options.bottomBoundary),
@@ -276,7 +276,8 @@ class Sticky extends Component {
276276
// This case only happens when Sticky's bottom sticks to the screen bottom and
277277
// its height gets changed. Sticky should be in RELEASE status and update its
278278
// sticky bottom by calculating how much height it changed.
279-
var deltaHeight = pos + height - this.state.bottom;
279+
const deltaHeight =
280+
pos + height - this.state.bottom;
280281
this.stickyBottom = bottom - delta + deltaHeight;
281282
this.stickyTop = this.stickyBottom - height;
282283
} else {
@@ -338,7 +339,7 @@ class Sticky extends Component {
338339
}
339340

340341
componentWillUnmount() {
341-
var subscribers = this.subscribers || [];
342+
const subscribers = this.subscribers || [];
342343
for (var i = subscribers.length - 1; i >= 0; i--) {
343344
this.subscribers[i].unsubscribe();
344345
}
@@ -385,7 +386,7 @@ class Sticky extends Component {
385386
}
386387

387388
translate(style, pos) {
388-
var enableTransforms =
389+
const enableTransforms =
389390
canEnableTransforms && this.props.enableTransforms;
390391
if (enableTransforms && this.state.activated) {
391392
style[TRANSFORM_PROP] =
@@ -407,12 +408,12 @@ class Sticky extends Component {
407408

408409
render() {
409410
// TODO, "overflow: auto" prevents collapse, need a good way to get children height
410-
var innerStyle = {
411+
const innerStyle = {
411412
position: this.state.status === STATUS_FIXED ? 'fixed' : 'relative',
412413
top: this.state.status === STATUS_FIXED ? '0px' : '',
413414
zIndex: this.props.innerZ,
414415
};
415-
var outerStyle = {};
416+
const outerStyle = {};
416417

417418
// always use translate3d to enhance the performance
418419
this.translate(innerStyle, this.state.pos);
@@ -421,7 +422,7 @@ class Sticky extends Component {
421422
outerStyle.height = this.state.height + 'px';
422423
}
423424

424-
var outerClasses = classNames(
425+
const outerClasses = classNames(
425426
'sticky-outer-wrapper',
426427
this.props.className,
427428
{
@@ -431,7 +432,7 @@ class Sticky extends Component {
431432
}
432433
);
433434

434-
var innerClasses = classNames(
435+
const innerClasses = classNames(
435436
'sticky-inner-wrapper',
436437
this.props.innerClass,
437438
{
@@ -440,7 +441,7 @@ class Sticky extends Component {
440441
}
441442
);
442443

443-
var children = this.props.children;
444+
const children = this.props.children;
444445

445446
return (
446447
<div
@@ -513,4 +514,4 @@ Sticky.STATUS_ORIGINAL = STATUS_ORIGINAL;
513514
Sticky.STATUS_RELEASED = STATUS_RELEASED;
514515
Sticky.STATUS_FIXED = STATUS_FIXED;
515516

516-
module.exports = Sticky;
517+
export default Sticky;

tests/functional/sticky-functional.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const classNames = require('classnames');
2-
const Sticky = require('../../../dist/Sticky');
2+
const Sticky = require('../../../dist/cjs/Sticky');
33

44
let content = [];
55
for (let i = 0; i < 10; i++) {

tests/unit/Sticky.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ process.env.NODE_ENV = 'development';
1010
const ee = require('subscribe-ui-event/dist/globalVars').EE;
1111
const { render } = require('@testing-library/react');
1212
const React = require('react');
13-
const Sticky = require('../../dist/Sticky');
13+
const Sticky = require('../../dist/cjs/Sticky');
1414

1515
const STICKY_CLASS_OUTER = 'sticky-outer-wrapper';
1616
const STICKY_CLASS_INNER = 'sticky-inner-wrapper';

0 commit comments

Comments
 (0)