Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure correct DOM node order when performing focus actions #1038

Merged
merged 4 commits into from
Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixes

- Ensure portal root exists in the DOM ([#950](https://github.com/tailwindlabs/headlessui/pull/950))
- Ensure correct DOM node order when performing focus actions ([#1038](https://github.com/tailwindlabs/headlessui/pull/1038))

### Added

Expand All @@ -21,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Fix missing key binding in examples ([#1036](https://github.com/tailwindlabs/headlessui/pull/1036), [#1006](https://github.com/tailwindlabs/headlessui/pull/1006))
- Fix slice => splice typo in `Tabs` component ([#1037](https://github.com/tailwindlabs/headlessui/pull/1037), [#986](https://github.com/tailwindlabs/headlessui/pull/986))
- Ensure correct DOM node order when performing focus actions ([#1038](https://github.com/tailwindlabs/headlessui/pull/1038))

### Added

Expand Down
39 changes: 39 additions & 0 deletions packages/@headlessui-react/src/components/tabs/tabs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,45 @@ describe('Rendering', () => {
assertTabs({ active: 0 })
})

it('should guarantee the order of DOM nodes when performing actions', async () => {
function Example() {
let [hide, setHide] = useState(false)

return (
<>
<button onClick={() => setHide(v => !v)}>toggle</button>
<Tab.Group>
<Tab.List>
<Tab>Tab 1</Tab>
{!hide && <Tab>Tab 2</Tab>}
<Tab>Tab 3</Tab>
</Tab.List>

<Tab.Panels>
<Tab.Panel>Content 1</Tab.Panel>
{!hide && <Tab.Panel>Content 2</Tab.Panel>}
<Tab.Panel>Content 3</Tab.Panel>
</Tab.Panels>
</Tab.Group>
</>
)
}

render(<Example />)

await click(getByText('toggle')) // Remove Tab 2
await click(getByText('toggle')) // Re-add Tab 2

await press(Keys.Tab)
assertTabs({ active: 0 })

await press(Keys.ArrowRight)
assertTabs({ active: 1 })

await press(Keys.ArrowRight)
assertTabs({ active: 2 })
})

describe('`renderProps`', () => {
it('should expose the `selectedIndex` on the `Tab.Group` component', async () => {
render(
Expand Down
8 changes: 0 additions & 8 deletions packages/@headlessui-react/src/components/tabs/tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,6 @@ export function Tab<TTag extends ElementType = typeof DEFAULT_TAB_TAG>(
}
let passThroughProps = props

if (process.env.NODE_ENV === 'test') {
Object.assign(propsWeControl, { 'data-headlessui-index': myIndex })
}

return render({
props: { ...passThroughProps, ...propsWeControl },
slot,
Expand Down Expand Up @@ -433,10 +429,6 @@ function Panel<TTag extends ElementType = typeof DEFAULT_PANEL_TAG>(
tabIndex: selected ? 0 : -1,
}

if (process.env.NODE_ENV === 'test') {
Object.assign(propsWeControl, { 'data-headlessui-index': myIndex })
}

let passThroughProps = props

return render({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1224,8 +1224,8 @@ export function assertTabs(
expect(list).toHaveAttribute('role', 'tablist')
expect(list).toHaveAttribute('aria-orientation', orientation)

let activeTab = tabs.find(tab => tab.dataset.headlessuiIndex === '' + active)
let activePanel = panels.find(panel => panel.dataset.headlessuiIndex === '' + active)
let activeTab = Array.from(list.querySelectorAll('[id^="headlessui-tabs-tab-"]'))[active]
let activePanel = panels.find(panel => panel.id === activeTab.getAttribute('aria-controls'))

for (let tab of tabs) {
expect(tab).toHaveAttribute('id')
Expand Down
10 changes: 9 additions & 1 deletion packages/@headlessui-react/src/utils/focus-management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,15 @@ export function focusElement(element: HTMLElement | null) {
}

export function focusIn(container: HTMLElement | HTMLElement[], focus: Focus) {
let elements = Array.isArray(container) ? container : getFocusableElements(container)
let elements = Array.isArray(container)
? container.slice().sort((a, b) => {
let position = a.compareDocumentPosition(b)

if (position & Node.DOCUMENT_POSITION_FOLLOWING) return -1
if (position & Node.DOCUMENT_POSITION_PRECEDING) return 1
return 0
})
: getFocusableElements(container)
let active = document.activeElement as HTMLElement

let direction = (() => {
Expand Down
45 changes: 45 additions & 0 deletions packages/@headlessui-vue/src/components/tabs/tabs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,51 @@ describe('Rendering', () => {
assertTabs({ active: 0 })
})

it('should guarantee the order of DOM nodes when performing actions', async () => {
renderTemplate({
template: html`
<button @click="toggle()">toggle</button>
<TabGroup>
<TabList>
<Tab>Tab 1</Tab>
<Tab v-if="!hide">Tab 2</Tab>
<Tab>Tab 3</Tab>
</TabList>

<TabPanels>
<TabPanel>Content 1</TabPanel>
<TabPanel v-if="!hide">Content 2</TabPanel>
<TabPanel>Content 3</TabPanel>
</TabPanels>
</TabGroup>
`,
setup() {
let hide = ref(false)

return {
hide,
toggle() {
hide.value = !hide.value
},
}
},
})

await new Promise<void>(nextTick)

await click(getByText('toggle')) // Remove Tab 2
await click(getByText('toggle')) // Re-add Tab 2

await press(Keys.Tab)
assertTabs({ active: 0 })

await press(Keys.ArrowRight)
assertTabs({ active: 1 })

await press(Keys.ArrowRight)
assertTabs({ active: 2 })
})

describe('`renderProps`', () => {
it('should expose the `selectedIndex` on the `Tabs` component', async () => {
renderTemplate(
Expand Down
8 changes: 0 additions & 8 deletions packages/@headlessui-vue/src/components/tabs/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,6 @@ export let Tab = defineComponent({
disabled: this.$props.disabled ? true : undefined,
}

if (process.env.NODE_ENV === 'test') {
Object.assign(propsWeControl, { ['data-headlessui-index']: this.myIndex })
}

return render({
props: { ...this.$props, ...propsWeControl },
slot,
Expand Down Expand Up @@ -334,10 +330,6 @@ export let TabPanel = defineComponent({
tabIndex: this.selected ? 0 : -1,
}

if (process.env.NODE_ENV === 'test') {
Object.assign(propsWeControl, { ['data-headlessui-index']: this.myIndex })
}

return render({
props: { ...this.$props, ...propsWeControl },
slot,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1224,8 +1224,8 @@ export function assertTabs(
expect(list).toHaveAttribute('role', 'tablist')
expect(list).toHaveAttribute('aria-orientation', orientation)

let activeTab = tabs.find(tab => tab.dataset.headlessuiIndex === '' + active)
let activePanel = panels.find(panel => panel.dataset.headlessuiIndex === '' + active)
let activeTab = Array.from(list.querySelectorAll('[id^="headlessui-tabs-tab-"]'))[active]
let activePanel = panels.find(panel => panel.id === activeTab.getAttribute('aria-controls'))

for (let tab of tabs) {
expect(tab).toHaveAttribute('id')
Expand Down
10 changes: 9 additions & 1 deletion packages/@headlessui-vue/src/utils/focus-management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,15 @@ export function focusElement(element: HTMLElement | null) {
}

export function focusIn(container: HTMLElement | HTMLElement[], focus: Focus) {
let elements = Array.isArray(container) ? container : getFocusableElements(container)
let elements = Array.isArray(container)
? container.slice().sort((a, b) => {
let position = a.compareDocumentPosition(b)

if (position & Node.DOCUMENT_POSITION_FOLLOWING) return -1
if (position & Node.DOCUMENT_POSITION_PRECEDING) return 1
return 0
})
: getFocusableElements(container)
let active = document.activeElement as HTMLElement

let direction = (() => {
Expand Down