From 920365c1b706008c37e2926d99ebeda129e0d072 Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Fri, 26 Aug 2022 09:26:09 -0400 Subject: [PATCH] Stop scrolling when hitting end of focus trap (#1789) * wip * wip * fix it * fixit * update changelog --- packages/@headlessui-react/CHANGELOG.md | 1 + .../@headlessui-react/src/internal/hidden.tsx | 6 ++- packages/@headlessui-vue/CHANGELOG.md | 1 + .../@headlessui-vue/src/internal/hidden.ts | 6 ++- .../pages/dialog/dialog-focus-issue.tsx | 46 +++++++++++++++++++ 5 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 packages/playground-react/pages/dialog/dialog-focus-issue.tsx diff --git a/packages/@headlessui-react/CHANGELOG.md b/packages/@headlessui-react/CHANGELOG.md index bbfb72032f..4458507a65 100644 --- a/packages/@headlessui-react/CHANGELOG.md +++ b/packages/@headlessui-react/CHANGELOG.md @@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Only select the active option when using "singular" mode when pressing `` in the `Combobox` component ([#1750](https://github.com/tailwindlabs/headlessui/pull/1750)) - Improve the types of the `Combobox` component ([#1761](https://github.com/tailwindlabs/headlessui/pull/1761)) - Only restore focus to the `Menu.Button` if necessary when activating a `Menu.Option` ([#1782](https://github.com/tailwindlabs/headlessui/pull/1782)) +- Don't scroll when wrapping around in focus trap ([#1789](https://github.com/tailwindlabs/headlessui/pull/1789)) ## Changed diff --git a/packages/@headlessui-react/src/internal/hidden.tsx b/packages/@headlessui-react/src/internal/hidden.tsx index 4e9ffac056..398775c026 100644 --- a/packages/@headlessui-react/src/internal/hidden.tsx +++ b/packages/@headlessui-react/src/internal/hidden.tsx @@ -23,9 +23,11 @@ export let Hidden = forwardRefWithAs(function VisuallyHidden< ref, 'aria-hidden': (features & Features.Focusable) === Features.Focusable ? true : undefined, style: { - position: 'absolute', + position: 'fixed', + top: 1, + left: 1, width: 1, - height: 1, + height: 0, padding: 0, margin: -1, overflow: 'hidden', diff --git a/packages/@headlessui-vue/CHANGELOG.md b/packages/@headlessui-vue/CHANGELOG.md index 4aec5c1605..937116a7fc 100644 --- a/packages/@headlessui-vue/CHANGELOG.md +++ b/packages/@headlessui-vue/CHANGELOG.md @@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Improve `Combobox` re-opening keyboard issue on mobile ([#1732](https://github.com/tailwindlabs/headlessui/pull/1732)) - Only select the active option when using "singular" mode when pressing `` in the `Combobox` component ([#1750](https://github.com/tailwindlabs/headlessui/pull/1750)) - Only restore focus to the `MenuButton` if necessary when activating a `MenuOption` ([#1782](https://github.com/tailwindlabs/headlessui/pull/1782)) +- Don't scroll when wrapping around in focus trap ([#1789](https://github.com/tailwindlabs/headlessui/pull/1789)) ## [1.6.7] - 2022-07-12 diff --git a/packages/@headlessui-vue/src/internal/hidden.ts b/packages/@headlessui-vue/src/internal/hidden.ts index a4e655e3e4..a6599d4f8e 100644 --- a/packages/@headlessui-vue/src/internal/hidden.ts +++ b/packages/@headlessui-vue/src/internal/hidden.ts @@ -24,9 +24,11 @@ export let Hidden = defineComponent({ let ourProps = { 'aria-hidden': (features & Features.Focusable) === Features.Focusable ? true : undefined, style: { - position: 'absolute', + position: 'fixed', + top: 1, + left: 1, width: 1, - height: 1, + height: 0, padding: 0, margin: -1, overflow: 'hidden', diff --git a/packages/playground-react/pages/dialog/dialog-focus-issue.tsx b/packages/playground-react/pages/dialog/dialog-focus-issue.tsx new file mode 100644 index 0000000000..ad139c9204 --- /dev/null +++ b/packages/playground-react/pages/dialog/dialog-focus-issue.tsx @@ -0,0 +1,46 @@ +import { useState } from 'react' +import { Dialog } from '@headlessui/react' + +function Modal(props) { + return ( + +
+
+
+ + + + +
+
+
+ ) +} + +export default function DialogFocusIssue() { + let [isOpen, setIsOpen] = useState(false) + + return ( +
+

Headless UI Focus Jump

+ +
+
+
+
+
+
+
+
+
+
+
+ setIsOpen(false)} /> +
+ ) +}