-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add useStyles(style) hook for React.js (#159)
* feat: useStyles to support Hooks * Add note of `useStyles` to docs
- Loading branch information
1 parent
bd5af84
commit c647f58
Showing
4 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Isomorphic CSS style loader for Webpack | ||
* | ||
* Copyright © 2015-present Kriasoft, LLC. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE.txt file in the root directory of this source tree. | ||
*/ | ||
|
||
import { useContext, useEffect } from 'react' | ||
import StyleContext from './StyleContext' | ||
|
||
// To detect if it's in SSR process or in browser. Wrapping with | ||
// the function makes rollup's replacement of "this" avoidable | ||
// eslint-disable-next-line func-names | ||
const isBrowser = (function() { | ||
return this && typeof this.window === 'object' | ||
})() | ||
|
||
function useStyles(...styles) { | ||
const { insertCss } = useContext(StyleContext) | ||
if (!insertCss) throw new Error('Please provide "insertCss" function by StyleContext.Provider') | ||
const runEffect = () => { | ||
const removeCss = insertCss(...styles) | ||
return () => { | ||
setTimeout(removeCss, 0) | ||
} | ||
} | ||
if (isBrowser) { | ||
useEffect(runEffect, []) | ||
} else { | ||
runEffect() | ||
} | ||
} | ||
|
||
export default useStyles |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Isomorphic CSS style loader for Webpack | ||
* | ||
* Copyright © 2015-present Kriasoft, LLC. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE.txt file in the root directory of this source tree. | ||
*/ | ||
|
||
import React, { Component, Children } from 'react' | ||
import PropTypes from 'prop-types' | ||
import ReactDOM from 'react-dom' | ||
import useStyles from '../src/useStyles' | ||
import StyleContext from '../src/StyleContext' | ||
|
||
describe('useStyles(...styles)', () => { | ||
it('Should call insertCss and removeCss functions provided by context', () => { | ||
class Provider extends Component { | ||
render() { | ||
const { insertCss, children } = this.props | ||
return ( | ||
<StyleContext.Provider value={{ insertCss }}> | ||
{Children.only(children)} | ||
</StyleContext.Provider> | ||
) | ||
} | ||
} | ||
|
||
Provider.propTypes = { | ||
insertCss: PropTypes.func.isRequired, | ||
children: PropTypes.node.isRequired, | ||
} | ||
|
||
const FooWithStyles = () => { | ||
useStyles('') | ||
return <div /> | ||
} | ||
|
||
const insertCss = jest.fn(() => {}) | ||
const container = global.document.createElement('div') | ||
|
||
ReactDOM.render( | ||
<Provider insertCss={insertCss}> | ||
<FooWithStyles /> | ||
</Provider>, | ||
container, | ||
) | ||
ReactDOM.unmountComponentAtNode(container) | ||
expect(insertCss).toBeCalledTimes(1) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters