Skip to content

Commit f8d7132

Browse files
committed
feat: first version
1 parent eac9844 commit f8d7132

File tree

10 files changed

+8034
-0
lines changed

10 files changed

+8034
-0
lines changed

.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["@babel/preset-env", "@babel/preset-react"]
3+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
/dist/

.npmignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
!/dist/**/*.js
3+
!/src/**/*.js
4+
*.test.js

.travis.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
language: node_js
2+
3+
node_js:
4+
- 10
5+
6+
notifications:
7+
email: false
8+
9+
cache:
10+
yarn: true
11+
directories:
12+
- 'node_modules'
13+
14+
git:
15+
depth: 5

LICENSE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2019 Smooth Code
2+
3+
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:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
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.

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# react-merge-refs
2+
3+
[![License](https://img.shields.io/npm/l/react-merge-refs.svg)](https://github.com/smooth-code/react-merge-refs/blob/master/LICENSE)
4+
[![npm package](https://img.shields.io/npm/v/react-merge-refs/latest.svg)](https://www.npmjs.com/package/react-merge-refs)
5+
[![Build Status](https://img.shields.io/travis/smooth-code/react-merge-refs.svg)](https://travis-ci.org/smooth-code/react-merge-refs)
6+
[![DevDependencies](https://img.shields.io/david/dev/smooth-code/react-merge-refs.svg)](https://david-dm.org/smooth-code/react-merge-refs?type=dev)
7+
8+
React utility to merge refs 🖇
9+
10+
```sh
11+
npm install react-merge-refs
12+
```
13+
14+
## Example
15+
16+
```js
17+
import React from 'react'
18+
import mergeRefs from 'react-merge-refs'
19+
20+
const Example = React.forwardRef(function Example(props, ref) {
21+
const localRef = React.useRef()
22+
return <div ref={mergeRefs([localRef, ref])} />
23+
})
24+
```
25+
26+
## Why?
27+
28+
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.
29+
30+
Today a `ref` can be a `function` or an `object`, tomorrow it could be another thing, who knows. This utility handles compatibility for you.
31+
32+
# License
33+
34+
MIT

package.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "react-merge-refs",
3+
"description": "React utility to merge refs.",
4+
"keywords": [
5+
"react",
6+
"utility",
7+
"ref"
8+
],
9+
"version": "1.0.0",
10+
"author": "Greg Bergé <[email protected]>",
11+
"license": "MIT",
12+
"main": "dist/index.js",
13+
"umd:main": "dist/index.umd.js",
14+
"module": "dist/index.mjs",
15+
"source": "src/index.js",
16+
"scripts": {
17+
"test": "jest",
18+
"prebuild": "rm -rf dist/",
19+
"build": "microbundle --external react",
20+
"prepublishOnly": "yarn build",
21+
"release": "standard-version && conventional-github-releaser --preset angular"
22+
},
23+
"devDependencies": {
24+
"@babel/core": "^7.6.0",
25+
"@babel/preset-env": "^7.6.0",
26+
"@babel/preset-react": "^7.0.0",
27+
"@testing-library/react": "^9.1.4",
28+
"babel-core": "^7.0.0-0",
29+
"babel-jest": "^24.9.0",
30+
"conventional-github-releaser": "^3.1.3",
31+
"jest": "^24.9.0",
32+
"microbundle": "^0.11.0",
33+
"react": "^16.9.0",
34+
"react-dom": "^16.9.0",
35+
"standard-version": "^7.0.0"
36+
}
37+
}

src/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default function mergeRefs(refs) {
2+
return value => {
3+
refs.forEach(ref => {
4+
if (typeof ref === 'function') {
5+
ref(value)
6+
} else if (ref !== null && ref !== undefined) {
7+
ref.current = value
8+
}
9+
})
10+
}
11+
}

src/index.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import React from 'react'
2+
import { render } from '@testing-library/react'
3+
import mergeRefs from '.'
4+
5+
test('mergeRefs', () => {
6+
const Dummy = React.forwardRef(function Dummy(props, ref) {
7+
React.useImperativeHandle(ref, () => 'refValue')
8+
return null
9+
})
10+
const refAsFunc = jest.fn()
11+
const refAsObj = { current: undefined }
12+
function Example({ visible }) {
13+
return visible ? <Dummy ref={mergeRefs([refAsObj, refAsFunc])} /> : null
14+
}
15+
const { rerender } = render(<Example visible />)
16+
expect(refAsFunc).toHaveBeenCalledTimes(1)
17+
expect(refAsFunc).toHaveBeenCalledWith('refValue')
18+
expect(refAsObj.current).toBe('refValue')
19+
rerender(<Example visible={false} />)
20+
expect(refAsFunc).toHaveBeenCalledTimes(2)
21+
expect(refAsFunc).toHaveBeenCalledWith(null)
22+
expect(refAsObj.current).toBe(null)
23+
})

0 commit comments

Comments
 (0)