Skip to content

Commit

Permalink
Wrap document.startViewTransition with supports check
Browse files Browse the repository at this point in the history
  • Loading branch information
Inwerpsel committed Dec 25, 2024
1 parent 1b52fc1 commit f5d3f71
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 65 deletions.
118 changes: 59 additions & 59 deletions docs/demo/dist/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/demo/dist/bundle.js.map

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src/components/movable/MoveControls.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {AreasContext} from './MovablePanels';
import { Tutorial } from '../../_unstable/Tutorial';
import { MovableElementContext } from './MovableElement';
import { DragHandle } from './DragHandle';
import { doTransition } from '../../functions/viewTransition';

export function MoveControls() {
const {
Expand Down Expand Up @@ -41,7 +42,7 @@ export function MoveControls() {
</Checkbox>}
<Checkbox controls={[showMovers, setShowMovers]}>Move elements</Checkbox>
{Object.keys(uiState.map).length > 0 && (
<button onClick={() => document.startViewTransition(() => resetPanels())}>
<button onClick={() => doTransition(() => resetPanels())}>
reset
</button>
)}
Expand Down Expand Up @@ -86,7 +87,7 @@ export function MoveControls() {
if (name === '') {
return;
}
document.startViewTransition(() => {
doTransition(() => {
setUiState(JSON.parse(windowArrangments[name]));
});
}}
Expand Down
3 changes: 2 additions & 1 deletion src/components/ui/HistoryControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { Checkbox } from '../controls/Checkbox';
import { use } from '../../state';
import { Tutorial } from '../../_unstable/Tutorial';
import { icons } from '../../previewComponents';
import { doTransition } from '../../functions/viewTransition';

function HistoryBack() {
const { past, historyOffset } = useContext(HistoryNavigateContext);
Expand Down Expand Up @@ -285,7 +286,7 @@ export function scrollHistory(event) {
delta > 0 ? historyBack(delta, true) : historyForward(-delta, true);
// };
// if (now - lastScroll > 300 && Math.abs(delta) === 1) {
// document.startViewTransition(navigate);
// doTransition(navigate);
// } else {
// navigate();
// }
Expand Down
3 changes: 3 additions & 0 deletions src/functions/viewTransition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const supports = !!document.startViewTransition;

export const doTransition = supports ? (f => document.startViewTransition(f)) : (f: () => void) => f();
5 changes: 3 additions & 2 deletions src/hooks/useResumableReducer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { deleteStoredHistory, storeActions } from '../_unstable/historyStore';
import { saveAsJsonFile } from '../functions/export';
import { Action } from '../functions/reducerOf';
import { use } from '../state';
import { doTransition } from '../functions/viewTransition';

type Reducer<T> = (previous: T, action) => T

Expand Down Expand Up @@ -537,13 +538,13 @@ export function historyBackOne(): void {
// All of this happens even if the button does not move during the transition.
// So this uses a view transition mostly because it's a very clear demonstration of these drawbacks,
// and the other history UI makes it so you don't realy need these buttons anyway.
document.startViewTransition(() => {
doTransition(() => {
historyBack(1);
});
}

export function historyForwardOne(): void {
document.startViewTransition(() => {
doTransition(() => {
historyForward(1);
});
}
Expand Down

0 comments on commit f5d3f71

Please sign in to comment.