Skip to content

Commit 3580584

Browse files
authored
[playground] ViewTransition on tab switch (facebook#34596)
<!-- Thanks for submitting a pull request! We appreciate you spending the time to work on these changes. Please provide enough information so that others can review your pull request. The three fields below are mandatory. Before submitting a pull request, please make sure the following is done: 1. Fork [the repository](https://github.com/facebook/react) and create your branch from `main`. 2. Run `yarn` in the repository root. 3. If you've fixed a bug or added code that should be tested, add tests! 4. Ensure the test suite passes (`yarn test`). Tip: `yarn test --watch TestName` is helpful in development. 5. Run `yarn test --prod` to test in the production environment. It supports the same options as `yarn test`. 6. If you need a debugger, run `yarn test --debug --watch TestName`, open `chrome://inspect`, and press "Inspect". 7. Format your code with [prettier](https://github.com/prettier/prettier) (`yarn prettier`). 8. Make sure your code lints (`yarn lint`). Tip: `yarn linc` to only check changed files. 9. Run the [Flow](https://flowtype.org/) type checks (`yarn flow`). 10. If you haven't already, complete the CLA. Learn more about contributing: https://reactjs.org/docs/how-to-contribute.html --> ## Summary <!-- Explain the **motivation** for making this change. What existing problem does the pull request solve? --> Utilized `<ViewTransition>` to introduce a sliding animation upon switching between the Output and SourceMap tabs in the default playground view. ## How did you test this change? <!-- Demonstrate the code is solid. Example: The exact commands you ran and their output, screenshots / videos if the pull request changes the user interface. How exactly did you verify that your PR solves the issue you wanted to solve? If you leave this empty, your PR will very likely be closed. --> https://github.com/user-attachments/assets/1ac93482-8104-4f9a-887e-6adca3537dca
1 parent 319a786 commit 3580584

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

compiler/apps/playground/components/Editor/EditorImpl.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@ import BabelPluginReactCompiler, {
2424
printFunctionWithOutlined,
2525
type LoggerEvent,
2626
} from 'babel-plugin-react-compiler';
27-
import {useDeferredValue, useMemo} from 'react';
27+
import {
28+
useDeferredValue,
29+
useMemo,
30+
unstable_ViewTransition as ViewTransition,
31+
} from 'react';
2832
import {useStore} from '../StoreContext';
2933
import ConfigEditor from './ConfigEditor';
3034
import Input from './Input';

compiler/apps/playground/components/TabbedWindow.tsx

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*/
7-
import React from 'react';
7+
import React, {
8+
startTransition,
9+
useId,
10+
unstable_ViewTransition as ViewTransition,
11+
unstable_addTransitionType as addTransitionType,
12+
} from 'react';
813
import clsx from 'clsx';
14+
import {TOGGLE_TAB_TRANSITION} from '../lib/transitionTypes';
915

1016
export default function TabbedWindow({
1117
tabs,
@@ -16,6 +22,16 @@ export default function TabbedWindow({
1622
activeTab: string;
1723
onTabChange: (tab: string) => void;
1824
}): React.ReactElement {
25+
const id = useId();
26+
const transitionName = `tab-highlight-${id}`;
27+
28+
const handleTabChange = (tab: string): void => {
29+
startTransition(() => {
30+
addTransitionType(TOGGLE_TAB_TRANSITION);
31+
onTabChange(tab);
32+
});
33+
};
34+
1935
return (
2036
<div className="flex-1 min-w-[550px] sm:min-w-0">
2137
<div className="flex flex-col h-full max-w-full">
@@ -25,13 +41,29 @@ export default function TabbedWindow({
2541
return (
2642
<button
2743
key={tab}
28-
onClick={() => onTabChange(tab)}
44+
onClick={() => handleTabChange(tab)}
2945
className={clsx(
30-
'active:scale-95 transition-transform py-1.5 px-1.5 xs:px-3 sm:px-4 rounded-full text-sm',
31-
!isActive && 'hover:bg-primary/5',
32-
isActive && 'bg-highlight text-link',
46+
'transition-transform py-1.5 px-1.5 xs:px-3 sm:px-4 rounded-full text-sm relative',
47+
isActive ? 'text-link' : 'hover:bg-primary/5',
3348
)}>
34-
{tab}
49+
{isActive && (
50+
<ViewTransition
51+
name={transitionName}
52+
share={{
53+
[TOGGLE_TAB_TRANSITION]: 'tab-highlight',
54+
default: 'none',
55+
}}
56+
update={{default: 'none'}}>
57+
<div className="absolute inset-0 bg-highlight rounded-full" />
58+
</ViewTransition>
59+
)}
60+
<ViewTransition
61+
update={{
62+
[TOGGLE_TAB_TRANSITION]: 'tab-text',
63+
default: 'none',
64+
}}>
65+
<span className="relative z-1">{tab}</span>
66+
</ViewTransition>
3567
</button>
3668
);
3769
})}

compiler/apps/playground/lib/transitionTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
*/
77

88
export const CONFIG_PANEL_TRANSITION = 'config-panel';
9+
export const TOGGLE_TAB_TRANSITION = 'toggle-tab';

compiler/apps/playground/styles/globals.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,11 @@
108108
object-fit: none;
109109
object-position: left;
110110
}
111+
112+
::view-transition-old(.tab-highlight),
113+
::view-transition-new(.tab-highlight) {
114+
height: 100%;
115+
}
116+
::view-transition-group(.tab-text) {
117+
z-index: 1;
118+
}

0 commit comments

Comments
 (0)