Skip to content

Commit

Permalink
Expose close functionality via render prop (#697)
Browse files Browse the repository at this point in the history
* expose a `close` function via the render prop for the `Popover` and `Popover.Panel` components (React)

* expose a `close` function via the render prop for the `Disclosure` and `Disclosure.Panel` components (React)

* expose a `close` function via the render prop for the `Popover` and `PopoverPanel` components (Vue)

* expose a `close` function via the render prop for the `Disclosure` and `DisclosurePanel` components (Vue)
  • Loading branch information
RobinMalfait committed Jul 26, 2021
1 parent e830338 commit ba1bd52
Show file tree
Hide file tree
Showing 8 changed files with 1,062 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { createElement, useEffect } from 'react'
import React, { createElement, useEffect, useRef } from 'react'
import { render } from '@testing-library/react'

import { Disclosure } from './disclosure'
Expand Down Expand Up @@ -115,6 +115,127 @@ describe('Rendering', () => {

assertDisclosureButton({ state: DisclosureState.InvisibleUnmounted })
})

it(
'should expose a close function that closes the disclosure',
suppressConsoleLogs(async () => {
render(
<Disclosure>
{({ close }) => (
<>
<Disclosure.Button>Trigger</Disclosure.Button>
<Disclosure.Panel>
<button onClick={() => close()}>Close me</button>
</Disclosure.Panel>
</>
)}
</Disclosure>
)

// Focus the button
getDisclosureButton()?.focus()

// Ensure the button is focused
assertActiveElement(getDisclosureButton())

// Open the disclosure
await click(getDisclosureButton())

// Ensure we can click the close button
await click(getByText('Close me'))

// Ensure the disclosure is closed
assertDisclosurePanel({ state: DisclosureState.InvisibleUnmounted })

// Ensure the Disclosure.Button got the restored focus
assertActiveElement(getByText('Trigger'))
})
)

it(
'should expose a close function that closes the disclosure and restores to a specific element',
suppressConsoleLogs(async () => {
render(
<>
<button id="test">restoreable</button>
<Disclosure>
{({ close }) => (
<>
<Disclosure.Button>Trigger</Disclosure.Button>
<Disclosure.Panel>
<button onClick={() => close(document.getElementById('test')!)}>
Close me
</button>
</Disclosure.Panel>
</>
)}
</Disclosure>
</>
)

// Focus the button
getDisclosureButton()?.focus()

// Ensure the button is focused
assertActiveElement(getDisclosureButton())

// Open the disclosure
await click(getDisclosureButton())

// Ensure we can click the close button
await click(getByText('Close me'))

// Ensure the disclosure is closed
assertDisclosurePanel({ state: DisclosureState.InvisibleUnmounted })

// Ensure the restoreable button got the restored focus
assertActiveElement(getByText('restoreable'))
})
)

it(
'should expose a close function that closes the disclosure and restores to a ref',
suppressConsoleLogs(async () => {
function Example() {
let elementRef = useRef(null)
return (
<>
<button ref={elementRef}>restoreable</button>
<Disclosure>
{({ close }) => (
<>
<Disclosure.Button>Trigger</Disclosure.Button>
<Disclosure.Panel>
<button onClick={() => close(elementRef)}>Close me</button>
</Disclosure.Panel>
</>
)}
</Disclosure>
</>
)
}

render(<Example />)

// Focus the button
getDisclosureButton()?.focus()

// Ensure the button is focused
assertActiveElement(getDisclosureButton())

// Open the disclosure
await click(getDisclosureButton())

// Ensure we can click the close button
await click(getByText('Close me'))

// Ensure the disclosure is closed
assertDisclosurePanel({ state: DisclosureState.InvisibleUnmounted })

// Ensure the restoreable button got the restored focus
assertActiveElement(getByText('restoreable'))
})
)
})

describe('Disclosure.Button', () => {
Expand Down Expand Up @@ -242,6 +363,115 @@ describe('Rendering', () => {
assertDisclosureButton({ state: DisclosureState.InvisibleHidden })
assertDisclosurePanel({ state: DisclosureState.InvisibleHidden })
})

it(
'should expose a close function that closes the disclosure',
suppressConsoleLogs(async () => {
render(
<Disclosure>
<Disclosure.Button>Trigger</Disclosure.Button>
<Disclosure.Panel>
{({ close }) => <button onClick={() => close()}>Close me</button>}
</Disclosure.Panel>
</Disclosure>
)

// Focus the button
getDisclosureButton()?.focus()

// Ensure the button is focused
assertActiveElement(getDisclosureButton())

// Open the disclosure
await click(getDisclosureButton())

// Ensure we can click the close button
await click(getByText('Close me'))

// Ensure the disclosure is closed
assertDisclosurePanel({ state: DisclosureState.InvisibleUnmounted })

// Ensure the Disclosure.Button got the restored focus
assertActiveElement(getByText('Trigger'))
})
)

it(
'should expose a close function that closes the disclosure and restores to a specific element',
suppressConsoleLogs(async () => {
render(
<>
<button id="test">restoreable</button>
<Disclosure>
<Disclosure.Button>Trigger</Disclosure.Button>
<Disclosure.Panel>
{({ close }) => (
<button onClick={() => close(document.getElementById('test')!)}>Close me</button>
)}
</Disclosure.Panel>
</Disclosure>
</>
)

// Focus the button
getDisclosureButton()?.focus()

// Ensure the button is focused
assertActiveElement(getDisclosureButton())

// Open the disclosure
await click(getDisclosureButton())

// Ensure we can click the close button
await click(getByText('Close me'))

// Ensure the disclosure is closed
assertDisclosurePanel({ state: DisclosureState.InvisibleUnmounted })

// Ensure the restoreable button got the restored focus
assertActiveElement(getByText('restoreable'))
})
)

it(
'should expose a close function that closes the disclosure and restores to a ref',
suppressConsoleLogs(async () => {
function Example() {
let elementRef = useRef(null)
return (
<>
<button ref={elementRef}>restoreable</button>
<Disclosure>
<Disclosure.Button>Trigger</Disclosure.Button>
<Disclosure.Panel>
{({ close }) => <button onClick={() => close(elementRef)}>Close me</button>}
</Disclosure.Panel>
</Disclosure>
</>
)
}

render(<Example />)

// Focus the button
getDisclosureButton()?.focus()

// Ensure the button is focused
assertActiveElement(getDisclosureButton())

// Open the disclosure
await click(getDisclosureButton())

// Ensure we can click the close button
await click(getByText('Close me'))

// Ensure the disclosure is closed
assertDisclosurePanel({ state: DisclosureState.InvisibleUnmounted })

// Ensure the restoreable button got the restored focus
assertActiveElement(getByText('restoreable'))
})
)
})
})

Expand Down
Loading

0 comments on commit ba1bd52

Please sign in to comment.