Skip to content

Commit

Permalink
feat: first version
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Sep 19, 2019
1 parent eac9844 commit f8d7132
Show file tree
Hide file tree
Showing 10 changed files with 8,034 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["@babel/preset-env", "@babel/preset-react"]
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
/dist/
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
!/dist/**/*.js
!/src/**/*.js
*.test.js
15 changes: 15 additions & 0 deletions .travis.yml
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
7 changes: 7 additions & 0 deletions LICENSE
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.
34 changes: 34 additions & 0 deletions README.md
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
37 changes: 37 additions & 0 deletions package.json
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"
}
}
11 changes: 11 additions & 0 deletions src/index.js
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
}
})
}
}
23 changes: 23 additions & 0 deletions src/index.test.js
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)
})
Loading

0 comments on commit f8d7132

Please sign in to comment.