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

Add useScaleCanvas: Web Animations API for zoom in/out animation #66917

Merged
merged 64 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 62 commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
af08ecc
Midway commit. Scaling out is broken
jeryj Oct 30, 2024
c83c99f
Fix zoom in animation
jeryj Oct 30, 2024
ec58902
Fix scaling by preserving CSS properites
jeryj Oct 30, 2024
6582cce
Prevent reflow from removing and re adding scrollbar in animation
jeryj Oct 30, 2024
ce9497e
Only rerun zoom out use effects if zoom out has changed.
jeryj Oct 31, 2024
26d9e3a
Allow all CSS vars to update when scale changes
jeryj Oct 31, 2024
3947ab0
Midway commit. Math is wrong for addressing top/bottom exceptions
jeryj Oct 31, 2024
cb47e82
Remove usePrevious usage
ajlende Oct 31, 2024
bc8cca6
Add prefers-reduced-motion to setTimeout delay
ajlende Nov 5, 2024
578c923
WIP Working zoom without frame size
ajlende Nov 5, 2024
d2c16a6
Account for changes to client height when determining edge threshold
jeryj Nov 6, 2024
0faf418
Zoom to center unless it will reveal top or bottom
jeryj Nov 6, 2024
ab8038e
Account for a top threshold when zooming in and out
jeryj Nov 6, 2024
ba32f99
Clean up math and add comments
ajlende Nov 6, 2024
0b640ee
Add event listener instead of timeout
ajlende Nov 6, 2024
6a3f7a0
Fix reduced motion
ajlende Nov 6, 2024
55df6fe
Remove timeout ref
ajlende Nov 6, 2024
7ba67b2
Refactor callback as separate effect
ajlende Nov 6, 2024
2dc137f
Try to add back useEffect cleanups
ajlende Nov 6, 2024
3eabcf5
Initialize prevClientHeight in the useEffect
ajlende Nov 7, 2024
8b8c206
use useReducedMotion
jeryj Nov 7, 2024
23bd453
Add test for zoom in/out location
jeryj Nov 7, 2024
8cbe2be
Hack to fix reduced motion
ajlende Nov 8, 2024
4c9543e
Clean up the frameSizeValue and scaleValue calculations
ajlende Nov 8, 2024
4e87729
Replace TODO comments with HACK comments
ajlende Nov 8, 2024
17f50d5
Add cleanup for raf
ajlende Nov 8, 2024
b049f4b
Simplify CSS diff for 6.7 review
ajlende Nov 8, 2024
61a0de9
Add one more HACK comment
ajlende Nov 8, 2024
ac52f2d
Do not allow scrollTopNext to be smaller than 0
jeryj Nov 11, 2024
6b23a30
Fix zoom-out.spec.js
ajlende Nov 19, 2024
fd74dc4
Move 6.7 iframe scaling work to trunk
jeryj Nov 13, 2024
bfaf7e4
Move calculations to useScaleCanvas hook
jeryj Nov 13, 2024
cc28155
Rename scaleValue to scale
jeryj Nov 13, 2024
d407ee6
Rename frameSizeValue to frameSize
jeryj Nov 13, 2024
c8f2cc7
Remove iframeWindowInnerHeight
jeryj Nov 13, 2024
7903702
Remove unused window resize ref
jeryj Nov 13, 2024
72f5785
Refactor CSS
jeryj Nov 13, 2024
53d8f6f
Move animation to state
jeryj Nov 13, 2024
a25f7da
Fix scaling the canvas when sidebars open/close
jeryj Nov 13, 2024
5fcd9c1
Recalculate scale before exiting scaled canvas to prevent snapping ef…
jeryj Nov 14, 2024
1c5d7d5
CSS linting
jeryj Nov 14, 2024
96d62ab
Move code into useScaleCanvas that isn't necessary in iframe index
jeryj Nov 14, 2024
ec37f26
Move resize listeners into useScaledCanvas
jeryj Nov 14, 2024
28cff48
Combine into one useEffect
jeryj Nov 15, 2024
28ded6a
Add useEffect cleanup
jeryj Nov 15, 2024
1dbdc82
Fix accidental setting prevScaleRef to scale instead of scaleValue
jeryj Nov 19, 2024
cf8ffde
Switch to animations api
jeryj Nov 19, 2024
f338821
Reduce rerenders by removing isAnimatingZoomOut state and relying jus…
jeryj Nov 19, 2024
5c93399
Refactor to set scale and frame size on transition end
jeryj Nov 19, 2024
ff03e1e
Add reverse animation if zoom state is toggled quickly
jeryj Nov 20, 2024
65bd82e
Refactor step 1 of reverse animation with transitionTo and transition…
jeryj Nov 20, 2024
f236641
Finish refactor of transitionTo/From refs
jeryj Nov 20, 2024
83ee4c5
Refactor: computeScrollTopNext
jeryj Nov 20, 2024
32b24e5
Refactor: getAnimationKeyframes
jeryj Nov 20, 2024
1407f8f
Add missing dependency
jeryj Nov 20, 2024
d5402bc
Refactor: create startZoomOutAnimation and rename scrollTopNext to tr…
jeryj Nov 20, 2024
fc6b4ee
Remove unneeded variables
jeryj Nov 20, 2024
2325135
Move CSS back to original location, as it no longer needs to be moved
jeryj Nov 21, 2024
b41d282
Add comments
jeryj Nov 21, 2024
0ff92b5
Fix eslint error
ajlende Nov 25, 2024
92e0e52
Move scrollHeight to animateFrom state
ajlende Nov 25, 2024
fb16c4f
Update JSDoc
ajlende Nov 25, 2024
4076a7b
Rename ref variables to use "Ref" suffix
ajlende Nov 26, 2024
798549e
Comment style guide
ajlende Nov 26, 2024
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
5 changes: 0 additions & 5 deletions packages/base-styles/_animations.scss
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,3 @@
@warn "The `edit-post__fade-in-animation` mixin is deprecated. Use `animation__fade-in` instead.";
@include animation__fade-in($speed, $delay);
}

@mixin editor-canvas-resize-animation($additional-transition-rules...) {
transition: all 400ms cubic-bezier(0.46, 0.03, 0.52, 0.96), $additional-transition-rules;
@include reduce-motion("transition");
}
Comment on lines -44 to -48
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the mixin, as it's only used once.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ iframe[name="editor-canvas"] {
height: 100%;
display: block;
// Handles transitions between device previews
@include editor-canvas-resize-animation;
transition: all 400ms cubic-bezier(0.46, 0.03, 0.52, 0.96);
@include reduce-motion("transition");
background-color: $gray-300;
}
75 changes: 34 additions & 41 deletions packages/block-editor/src/components/iframe/content.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

.block-editor-iframe__html {
transform-origin: top center;
// We don't want to animate the transform of the translateX because it is used
// to "center" the canvas. Leaving it on causes the canvas to slide around in
// odd ways.
@include editor-canvas-resize-animation( transform 0s, scale 0s, padding 0s, translate 0s);
// Prevents a flash of background color change when entering/exiting zoom out
transition: background-color 400ms;

&.zoom-out-animation {
$scroll-top: var(--wp-block-editor-iframe-zoom-out-scroll-top, 0);
Expand All @@ -18,53 +16,48 @@
right: 0;
top: calc(-1 * #{$scroll-top});
bottom: 0;
translate: 0 calc(#{$scroll-top} - #{$scroll-top-next});
// Force preserving a scrollbar gutter as scrollbar-gutter isn't supported in all browsers yet,
// and removing the scrollbar causes the content to shift.
overflow-y: scroll;

// We only want to animate the scaling when entering zoom out. When sidebars
// are toggled, the resizing of the iframe handles scaling the canvas as well,
// and the doubled animations cause very odd animations.
@include editor-canvas-resize-animation( transform 0s, top 0s, bottom 0s, right 0s, left 0s );
}
}

.block-editor-iframe__html.is-zoomed-out {
$scale: var(--wp-block-editor-iframe-zoom-out-scale);
$frame-size: var(--wp-block-editor-iframe-zoom-out-frame-size);
$inner-height: var(--wp-block-editor-iframe-zoom-out-inner-height);
$content-height: var(--wp-block-editor-iframe-zoom-out-content-height);
$scale-container-width: var(--wp-block-editor-iframe-zoom-out-scale-container-width);
$container-width: var(--wp-block-editor-iframe-zoom-out-container-width, 100vw);
// Apply an X translation to center the scaled content within the available space.
transform: translateX(calc((#{$scale-container-width} - #{$container-width}) / 2 / #{$scale}));
scale: #{$scale};
background-color: $gray-300;
&.is-zoomed-out {
$scale: var(--wp-block-editor-iframe-zoom-out-scale, 1);
$frame-size: var(--wp-block-editor-iframe-zoom-out-frame-size, 0);
$inner-height: var(--wp-block-editor-iframe-zoom-out-inner-height);
$content-height: var(--wp-block-editor-iframe-zoom-out-content-height);
$scale-container-width: var(--wp-block-editor-iframe-zoom-out-scale-container-width);
$container-width: var(--wp-block-editor-iframe-zoom-out-container-width, 100vw);
// Apply an X translation to center the scaled content within the available space.
transform: translateX(calc((#{$scale-container-width} - #{$container-width}) / 2 / #{$scale}));
scale: $scale;
background-color: $gray-300;

// Chrome seems to respect that transform scale shouldn't affect the layout size of the element,
// so we need to adjust the height of the content to match the scale by using negative margins.
$extra-content-height: calc(#{$content-height} * (1 - #{$scale}));
$total-frame-height: calc(2 * #{$frame-size} / #{$scale});
$total-height: calc(#{$extra-content-height} + #{$total-frame-height} + 2px);
margin-bottom: calc(-1 * #{$total-height});
// Add the top/bottom frame size. We use scaling to account for the left/right, as
// the padding left/right causes the contents to reflow, which breaks the 1:1 scaling
// of the content.
padding-top: calc(#{$frame-size} / #{$scale});
padding-bottom: calc(#{$frame-size} / #{$scale});
// Chrome seems to respect that transform scale shouldn't affect the layout size of the element,
// so we need to adjust the height of the content to match the scale by using negative margins.
$extra-content-height: calc(#{$content-height} * (1 - #{$scale}));
$total-frame-height: calc(2 * #{$frame-size} / #{$scale});
$total-height: calc(#{$extra-content-height} + #{$total-frame-height} + 2px);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is 2px?

Copy link
Contributor

@ajlende ajlende Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the same 2px from before; the line just got moved.

I'm not entirely sure what it does, but I assume it's a 1px border somewhere that isn't accounted for in the height.

margin-bottom: calc(-1 * #{$total-height});

body {
min-height: calc((#{$inner-height} - #{$total-frame-height}) / #{$scale});
// Add the top/bottom frame size. We use scaling to account for the left/right, as
// the padding left/right causes the contents to reflow, which breaks the 1:1 scaling
// of the content.
padding-top: calc(#{$frame-size} / #{$scale});
padding-bottom: calc(#{$frame-size} / #{$scale});

> .is-root-container:not(.wp-block-post-content) {
flex: 1;
display: flex;
flex-direction: column;
height: 100%;
body {
min-height: calc((#{$inner-height} - #{$total-frame-height}) / #{$scale});

> main {
> .is-root-container:not(.wp-block-post-content) {
flex: 1;
display: flex;
flex-direction: column;
height: 100%;

> main {
flex: 1;
}
}
}
}
Expand Down
Loading
Loading