-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from smooth-code/first-version
feat: first version
- Loading branch information
Showing
10 changed files
with
8,034 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": ["@babel/preset-env", "@babel/preset-react"] | ||
} |
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,2 @@ | ||
node_modules/ | ||
/dist/ |
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,4 @@ | ||
/* | ||
!/dist/**/*.js | ||
!/src/**/*.js | ||
*.test.js |
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,15 @@ | ||
language: node_js | ||
|
||
node_js: | ||
- 10 | ||
|
||
notifications: | ||
email: false | ||
|
||
cache: | ||
yarn: true | ||
directories: | ||
- 'node_modules' | ||
|
||
git: | ||
depth: 5 |
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,7 @@ | ||
Copyright 2019 Smooth Code | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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,34 @@ | ||
# react-merge-refs | ||
|
||
[![License](https://img.shields.io/npm/l/react-merge-refs.svg)](https://github.com/smooth-code/react-merge-refs/blob/master/LICENSE) | ||
[![npm package](https://img.shields.io/npm/v/react-merge-refs/latest.svg)](https://www.npmjs.com/package/react-merge-refs) | ||
[![Build Status](https://img.shields.io/travis/smooth-code/react-merge-refs.svg)](https://travis-ci.org/smooth-code/react-merge-refs) | ||
[![DevDependencies](https://img.shields.io/david/dev/smooth-code/react-merge-refs.svg)](https://david-dm.org/smooth-code/react-merge-refs?type=dev) | ||
|
||
React utility to merge refs 🖇 | ||
|
||
```sh | ||
npm install react-merge-refs | ||
``` | ||
|
||
## Example | ||
|
||
```js | ||
import React from 'react' | ||
import mergeRefs from 'react-merge-refs' | ||
|
||
const Example = React.forwardRef(function Example(props, ref) { | ||
const localRef = React.useRef() | ||
return <div ref={mergeRefs([localRef, ref])} /> | ||
}) | ||
``` | ||
|
||
## Why? | ||
|
||
When developing low level UI components, it is common to have to use a local ref but also support an external one using `React.forwardRef`. Natively, React does not offer a way to set two refs inside the `ref` property. This is the goal of this small utility. | ||
|
||
Today a `ref` can be a `function` or an `object`, tomorrow it could be another thing, who knows. This utility handles compatibility for you. | ||
|
||
# License | ||
|
||
MIT |
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,37 @@ | ||
{ | ||
"name": "react-merge-refs", | ||
"description": "React utility to merge refs.", | ||
"keywords": [ | ||
"react", | ||
"utility", | ||
"ref" | ||
], | ||
"version": "1.0.0", | ||
"author": "Greg Bergé <berge.greg@gmail.com>", | ||
"license": "MIT", | ||
"main": "dist/index.js", | ||
"umd:main": "dist/index.umd.js", | ||
"module": "dist/index.mjs", | ||
"source": "src/index.js", | ||
"scripts": { | ||
"test": "jest", | ||
"prebuild": "rm -rf dist/", | ||
"build": "microbundle --external react", | ||
"prepublishOnly": "yarn build", | ||
"release": "standard-version && conventional-github-releaser --preset angular" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.6.0", | ||
"@babel/preset-env": "^7.6.0", | ||
"@babel/preset-react": "^7.0.0", | ||
"@testing-library/react": "^9.1.4", | ||
"babel-core": "^7.0.0-0", | ||
"babel-jest": "^24.9.0", | ||
"conventional-github-releaser": "^3.1.3", | ||
"jest": "^24.9.0", | ||
"microbundle": "^0.11.0", | ||
"react": "^16.9.0", | ||
"react-dom": "^16.9.0", | ||
"standard-version": "^7.0.0" | ||
} | ||
} |
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,11 @@ | ||
export default function mergeRefs(refs) { | ||
return value => { | ||
refs.forEach(ref => { | ||
if (typeof ref === 'function') { | ||
ref(value) | ||
} else if (ref !== null && ref !== undefined) { | ||
ref.current = value | ||
} | ||
}) | ||
} | ||
} |
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,23 @@ | ||
import React from 'react' | ||
import { render } from '@testing-library/react' | ||
import mergeRefs from '.' | ||
|
||
test('mergeRefs', () => { | ||
const Dummy = React.forwardRef(function Dummy(props, ref) { | ||
React.useImperativeHandle(ref, () => 'refValue') | ||
return null | ||
}) | ||
const refAsFunc = jest.fn() | ||
const refAsObj = { current: undefined } | ||
function Example({ visible }) { | ||
return visible ? <Dummy ref={mergeRefs([refAsObj, refAsFunc])} /> : null | ||
} | ||
const { rerender } = render(<Example visible />) | ||
expect(refAsFunc).toHaveBeenCalledTimes(1) | ||
expect(refAsFunc).toHaveBeenCalledWith('refValue') | ||
expect(refAsObj.current).toBe('refValue') | ||
rerender(<Example visible={false} />) | ||
expect(refAsFunc).toHaveBeenCalledTimes(2) | ||
expect(refAsFunc).toHaveBeenCalledWith(null) | ||
expect(refAsObj.current).toBe(null) | ||
}) |
Oops, something went wrong.