From ee252b3e1a50b945b9b5536d8cecae94f91411fb Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Thu, 18 Apr 2024 17:44:07 +0200 Subject: [PATCH 01/10] use `Fragment` as the default element for `Transition` components --- .../@headlessui-react/src/components/transition/transition.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@headlessui-react/src/components/transition/transition.tsx b/packages/@headlessui-react/src/components/transition/transition.tsx index de66b8a426..a40c10cdec 100644 --- a/packages/@headlessui-react/src/components/transition/transition.tsx +++ b/packages/@headlessui-react/src/components/transition/transition.tsx @@ -264,7 +264,7 @@ function useNesting(done?: () => void, parent?: NestingContextValues) { // --- -let DEFAULT_TRANSITION_CHILD_TAG = 'div' as const +let DEFAULT_TRANSITION_CHILD_TAG = Fragment type TransitionChildRenderPropArg = MutableRefObject let TransitionChildRenderFeatures = RenderFeatures.RenderStrategy From c9b37d23dc9153898ba520e21926c69c10a3839b Mon Sep 17 00:00:00 2001 From: Robin Malfait Date: Thu, 18 Apr 2024 17:44:29 +0200 Subject: [PATCH 02/10] update tests to reflect default tag change --- .../__snapshots__/transition.test.tsx.snap | 6 - .../components/transition/transition.test.tsx | 128 ++++++++++-------- 2 files changed, 73 insertions(+), 61 deletions(-) diff --git a/packages/@headlessui-react/src/components/transition/__snapshots__/transition.test.tsx.snap b/packages/@headlessui-react/src/components/transition/__snapshots__/transition.test.tsx.snap index 1555d0302d..c00a2019cb 100644 --- a/packages/@headlessui-react/src/components/transition/__snapshots__/transition.test.tsx.snap +++ b/packages/@headlessui-react/src/components/transition/__snapshots__/transition.test.tsx.snap @@ -111,12 +111,6 @@ exports[`Setup API shallow should passthrough all the props (that we do not use `; -exports[`Setup API shallow should render a div and its children by default 1`] = ` -
- Children -
-`; - exports[`Setup API shallow should render another component if the \`as\` prop is used and its children by default 1`] = ` Children diff --git a/packages/@headlessui-react/src/components/transition/transition.test.tsx b/packages/@headlessui-react/src/components/transition/transition.test.tsx index ac4adfa414..5c915b8be4 100644 --- a/packages/@headlessui-react/src/components/transition/transition.test.tsx +++ b/packages/@headlessui-react/src/components/transition/transition.test.tsx @@ -1,5 +1,5 @@ import { act as _act, fireEvent, render } from '@testing-library/react' -import React, { Fragment, useEffect, useLayoutEffect, useRef, useState } from 'react' +import React, { useEffect, useLayoutEffect, useRef, useState } from 'react' import { getByText } from '../../test-utils/accessibility-assertions' import { executeTimeline } from '../../test-utils/execute-timeline' import { click } from '../../test-utils/interactions' @@ -22,7 +22,7 @@ function nextFrame() { it('should not steal the ref from the child', async () => { let fn = jest.fn() render( - +
...
) @@ -40,11 +40,6 @@ it('should render without crashing', () => { ) }) -it('should be possible to render a Transition without children', () => { - render() - expect(document.getElementsByClassName('transition')).not.toBeNull() -}) - it( 'should yell at us when we forget the required show prop', suppressConsoleLogs(() => { @@ -62,16 +57,15 @@ it( describe('Setup API', () => { describe('shallow', () => { - it('should render a div and its children by default', () => { - let { container } = render(Children) - - expect(container.firstChild).toMatchSnapshot() - }) - it('should passthrough all the props (that we do not use internally)', () => { let { container } = render( + /** + * Renders a Fragment by default and forwards props. But not possible to + * type in TypeScript land. This is also discouraged, but it works. + */ + // @ts-expect-error - Children +
Children
) @@ -99,7 +93,11 @@ describe('Setup API', () => { }) it('should render nothing when the show prop is false', () => { - let { container } = render(Children) + let { container } = render( + +
Children
+
+ ) expect(container.firstChild).toMatchSnapshot() }) @@ -115,11 +113,7 @@ describe('Setup API', () => { }) it('should be possible to use a render prop', () => { - let { container } = render( - - {() => Children} - - ) + let { container } = render({() => Children}) expect(container.firstChild).toMatchSnapshot() }) @@ -134,11 +128,7 @@ describe('Setup API', () => { } expect(() => { - render( - - {() => } - - ) + render({() => }) }).toThrowErrorMatchingSnapshot() }) ) @@ -153,7 +143,9 @@ describe('Setup API', () => { expect(() => { render(
- Oops + +
Oops
+
) }).toThrowErrorMatchingSnapshot() @@ -163,7 +155,9 @@ describe('Setup API', () => { it('should be possible to render a Transition.Child without children', () => { render( - + +
+ ) expect(document.getElementsByClassName('transition')).not.toBeNull() @@ -172,7 +166,9 @@ describe('Setup API', () => { it('should be possible to use a Transition.Root and a Transition.Child', () => { render( - + +
+ ) expect(document.getElementsByClassName('transition')).not.toBeNull() @@ -182,8 +178,14 @@ describe('Setup API', () => { let { container } = render(
- Sidebar - Content +
+ +
Sidebar
+
+ +
Content
+
+
) @@ -195,8 +197,10 @@ describe('Setup API', () => { let { container } = render(
- Sidebar - Content +
+ Sidebar + Content +
) @@ -221,8 +225,10 @@ describe('Setup API', () => { let { container } = render(
- {() => } - {() =>
Content
}
+
+ {() => } + {() =>
Content
}
+
) @@ -233,13 +239,11 @@ describe('Setup API', () => { it('should be possible to use render props on the Transition and Transition.Child components', () => { let { container } = render(
- + {() => (
- {() => } - - {() =>
Content
} -
+ {() => } + {() =>
Content
}
)}
@@ -262,8 +266,10 @@ describe('Setup API', () => { render(
- {() => Sidebar} - {() => Content} +
+ {() => Sidebar} + {() => Content} +
) @@ -283,7 +289,7 @@ describe('Setup API', () => { expect(() => { render(
- + {() => ( {() => } @@ -362,7 +368,7 @@ describe('Setup API', () => { leaveFrom="leave-from" leaveTo="leave-to" > - Children +
Children
) @@ -387,7 +393,7 @@ describe('Setup API', () => { leaveFrom="leave-from" leaveTo="leave-to" > - Children +
Children
) } @@ -732,10 +738,10 @@ describe('Transitions', () => { - I am fast +
I am fast
- I am slow +
I am slow
@@ -781,11 +787,11 @@ describe('Transitions', () => { I am fast - I am my own root component and I don't talk to the parent +
I am my own root component and I don't talk to the parent
- I am slow +
I am slow
@@ -953,12 +959,22 @@ describe('Events', () => { - Child 1. - - Child 2. - Child 2.1. - Child 2.2. - +
+ +
Child 1.
+
+ +
+
Child 2.
+ +
Child 2.1.
+
+ +
Child 2.2.
+
+
+
+
- + @@ -43,6 +43,7 @@ export default function Home() { function Box({ children }: { children?: ReactNode }) { return ( console.log('beforeEnter')} diff --git a/playgrounds/react/pages/transitions/full-page-examples/full-page-transition.tsx b/playgrounds/react/pages/transitions/full-page-examples/full-page-transition.tsx index 504e03b113..1095cdc5e7 100644 --- a/playgrounds/react/pages/transitions/full-page-examples/full-page-transition.tsx +++ b/playgrounds/react/pages/transitions/full-page-examples/full-page-transition.tsx @@ -160,6 +160,7 @@ function FullPageTransition() {
{pages.map((page, i) => ( {/* Off-canvas menu for mobile */} - + {/* Off-canvas menu overlay, show/hide based on off-canvas menu state. */} { {(t) => ( Date: Fri, 19 Apr 2024 10:11:08 -0400 Subject: [PATCH 10/10] Tweak changelog --- packages/@headlessui-react/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@headlessui-react/CHANGELOG.md b/packages/@headlessui-react/CHANGELOG.md index 67447d1a12..c7de49700c 100644 --- a/packages/@headlessui-react/CHANGELOG.md +++ b/packages/@headlessui-react/CHANGELOG.md @@ -30,7 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Use native `useId` and `useSyncExternalStore` hooks ([#3092](https://github.com/tailwindlabs/headlessui/pull/3092)) - Use `absolute` as the default Floating UI strategy ([#3097](https://github.com/tailwindlabs/headlessui/pull/3097)) - Change default tags for `ListboxOptions`, `ListboxOption`, `ComboboxOptions`, `ComboboxOption` and `TabGroup` components ([#3109](https://github.com/tailwindlabs/headlessui/pull/3109)) -- Change default `Transition` tag from `div` to `Fragment` ([#3110](https://github.com/tailwindlabs/headlessui/pull/3110)) +- Change default tag from `div` to `Fragment` on `Transition` components ([#3110](https://github.com/tailwindlabs/headlessui/pull/3110)) ### Added