Skip to content

Commit

Permalink
feat: add multiple sources support (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Mar 7, 2020
1 parent 1dd0283 commit a0c5239
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
18 changes: 18 additions & 0 deletions README.md
Expand Up @@ -94,6 +94,24 @@ function CustomTarget() {
}
```

### Use multiple sources

By default only one `Source` is allowed to be injected into a `Target`. Sometimes you may want to inject multiple sources into a single target. Create teleporter with `{ multiSources: true }` option.

```js
const Teleporter = createTeleporter({ multiSources: true })

<Teleporter.Source multiple>
<a href="#">A link</a>
</Teleporter.Source>

<Teleporter.Source multiple>
<a href="#">Another link</a>
</Teleporter.Source>

// The target will contains the two links
```

## API

### createTeleporter
Expand Down
6 changes: 4 additions & 2 deletions src/index.js
@@ -1,7 +1,7 @@
import React from 'react'
import ReactDOM from 'react-dom'

export function createTeleporter() {
export function createTeleporter({ multiSources } = {}) {
const context = {}

function setElement(element) {
Expand Down Expand Up @@ -30,7 +30,9 @@ export function createTeleporter() {

if (context.set) {
previousSet = context.set
context.set.current(null)
if (!multiSources) {
context.set.current(null)
}
}

context.set = setRef
Expand Down
18 changes: 18 additions & 0 deletions src/index.test.js
Expand Up @@ -250,6 +250,24 @@ describe('teleporter', () => {
expect(getByTestId('target')).toHaveTextContent('A')
})

it('allows multiple sources using `multiSources` option', () => {
const Teleporter = createTeleporter({ multiSources: true })

const { getByTestId } = render(
<div>
<div data-testid="target">
<Teleporter.Target />
</div>
<div>
<Teleporter.Source>A</Teleporter.Source>
<Teleporter.Source>B</Teleporter.Source>
</div>
</div>,
)

expect(getByTestId('target')).toHaveTextContent('AB')
})

it('handles uses the latest target defined', () => {
const Teleporter = createTeleporter()

Expand Down

0 comments on commit a0c5239

Please sign in to comment.