Skip to content

Commit 2103d03

Browse files
authored
Pool Emotion objects (#63)
* Pool emotion objects * Update entry * Update entry
1 parent 9e2b9d8 commit 2103d03

File tree

8 files changed

+33
-11
lines changed

8 files changed

+33
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
99

1010
### Added
1111

12-
- Support `nonce` prop for [Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy), in PR [#62](https://github.com/compulim/react-scroll-to-bottom/pull/62)
12+
- Support `nonce` prop for [Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy), in PR [#62](https://github.com/compulim/react-scroll-to-bottom/pull/62) and PR [#63](https://github.com/compulim/react-scroll-to-bottom/pull/63)
1313

1414
### Changed
1515

packages/component/package-lock.json

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

packages/component/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@
5555
"dependencies": {
5656
"classnames": "2.2.6",
5757
"create-emotion": "10.0.27",
58-
"prop-types": "15.7.2"
58+
"math-random": "2.0.1",
59+
"prop-types": "15.7.2",
60+
"simple-update-in": "2.2.0"
5961
},
6062
"peerDependencies": {
6163
"react": "^16.8.6"

packages/component/src/BasicScrollToBottom.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const BasicScrollToBottomCore = ({ children, className, followButtonClassName, s
1515
const rootCSS = useStyleToClassName()(ROOT_STYLE);
1616

1717
return (
18-
<div className={classNames(rootCSS + '', (className || '') + '')}>
18+
<div className={classNames(rootCSS, (className || '') + '')}>
1919
<Panel className={(scrollViewClassName || '') + ''}>{children}</Panel>
2020
<AutoHideFollowButton className={(followButtonClassName || '') + ''} />
2121
</div>
@@ -46,10 +46,6 @@ const BasicScrollToBottom = ({
4646
nonce,
4747
scrollViewClassName
4848
}) => {
49-
const styleToClassName = useStyleToClassName();
50-
51-
const rootCSS = styleToClassName(ROOT_STYLE);
52-
5349
return (
5450
<Composer checkInterval={checkInterval} debounce={debounce} mode={mode} nonce={nonce}>
5551
<BasicScrollToBottomCore

packages/component/src/ScrollToBottom/AutoHideFollowButton.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const AutoHideFollowButton = ({ children, className }) => {
3434

3535
return (
3636
!sticky && (
37-
<button className={classNames(rootCSS + '', (className || '') + '')} onClick={scrollToEnd} type="button">
37+
<button className={classNames(rootCSS, (className || '') + '')} onClick={scrollToEnd} type="button">
3838
{children}
3939
</button>
4040
)

packages/component/src/ScrollToBottom/Composer.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import createEmotion from 'create-emotion';
22
import PropTypes from 'prop-types';
33
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
44

5+
import createCSSKey from '../createCSSKey';
56
import EventSpy from '../EventSpy';
67
import FunctionContext from './FunctionContext';
78
import InternalContext from './InternalContext';
@@ -14,6 +15,10 @@ const MODE_TOP = 'top';
1415
const NEAR_END_THRESHOLD = 1;
1516
const SCROLL_DECISION_DURATION = 34; // 2 frames
1617

18+
// We pool the emotion object by nonce.
19+
// This is to make sure we don't generate too many unneeded <style> tags.
20+
const emotionPool = {};
21+
1722
function setImmediateInterval(fn, ms) {
1823
fn();
1924

@@ -259,9 +264,10 @@ const Composer = ({ checkInterval, children, debounce, mode, nonce }) => {
259264
);
260265

261266
const styleToClassName = useMemo(() => {
262-
const emotion = createEmotion({ key: 'rstb', nonce });
267+
const emotion =
268+
emotionPool[nonce] || (emotionPool[nonce] = createEmotion({ key: `rstb-${createCSSKey()}`, nonce }));
263269

264-
return emotion.css.bind(emotion);
270+
return style => emotion.css(style) + '';
265271
}, [nonce]);
266272

267273
const internalContext = useMemo(

packages/component/src/ScrollToBottom/Panel.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const Panel = ({ children, className }) => {
1616
const rootCSS = useStyleToClassName()(ROOT_STYLE);
1717

1818
return (
19-
<div className={classNames(rootCSS + '', (className || '') + '')} ref={setTarget}>
19+
<div className={classNames(rootCSS, (className || '') + '')} ref={setTarget}>
2020
{children}
2121
</div>
2222
);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import random from 'math-random';
2+
3+
export default function useCSSKey() {
4+
return random()
5+
.toString(26)
6+
.substr(2, 5)
7+
.replace(/\d/g, value => String.fromCharCode(value.charCodeAt(0) + 65));
8+
}

0 commit comments

Comments
 (0)