mirror of
https://github.com/outline/outline.git
synced 2026-12-06 11:12:32 +03:00
* perf: Reserve the right sidebar width at once when it opens The content beside the sidebar now reflows once on open, and the panel slides into place with a transform. * perf: Only reserve the sidebar width when the editor would reflow When the document still fits beside the sidebar, the width animates as before. The border moves with the sliding panel, and the open check is safe to run twice under StrictMode.
210 lines
6.3 KiB
TypeScript
210 lines
6.3 KiB
TypeScript
import type { Transition } from "framer-motion";
|
||
import { animate, m, useMotionValue } from "framer-motion";
|
||
import { observer } from "mobx-react";
|
||
import * as React from "react";
|
||
import styled, { useTheme } from "styled-components";
|
||
import { breakpoint } from "@shared/styles/breakpoint";
|
||
import { depths, s } from "@shared/styles";
|
||
import ErrorBoundary from "~/components/ErrorBoundary";
|
||
import ResizeBorder from "~/components/Sidebar/components/ResizeBorder";
|
||
import useStores from "~/hooks/useStores";
|
||
import useWindowScrollbarWidth from "~/hooks/useWindowScrollbarWidth";
|
||
import { useResizeHandle } from "~/hooks/useResizeHandle";
|
||
import { sidebarAppearDuration } from "~/styles/animations";
|
||
import { useDirection } from "radix-ui/direction";
|
||
|
||
interface Props extends React.HTMLAttributes<HTMLDivElement> {
|
||
children: React.ReactNode;
|
||
border?: boolean;
|
||
/** When true, skip the entrance animation and render at full width immediately. */
|
||
skipInitialAnimation?: boolean;
|
||
}
|
||
|
||
// No bounce, so the panel does not overshoot onto the content beside it.
|
||
const slideTransition: Transition = {
|
||
type: "spring",
|
||
bounce: 0,
|
||
duration: sidebarAppearDuration / 1000,
|
||
};
|
||
|
||
function Aside({ children, border, className, skipInitialAnimation }: Props) {
|
||
const theme = useTheme();
|
||
const { ui } = useStores();
|
||
const sidebarRef = React.useRef<HTMLDivElement>(null);
|
||
const positionRef = React.useRef<HTMLDivElement>(null);
|
||
const width = useMotionValue(skipInitialAnimation ? ui.sidebarRightWidth : 0);
|
||
const x = useMotionValue(0);
|
||
const maxWidth = theme.sidebarMaxWidth;
|
||
const minWidth = theme.sidebarResizeMinWidth;
|
||
const windowScrollbarWidth = useWindowScrollbarWidth();
|
||
const direction = useDirection();
|
||
|
||
const measure = React.useCallback(
|
||
(event: MouseEvent) => {
|
||
// Measure from the sidebar's own anchored edge rather than the window,
|
||
// as in a split view the sidebar is not positioned at the window edge.
|
||
const rect = positionRef.current?.getBoundingClientRect();
|
||
if (!rect) {
|
||
return undefined;
|
||
}
|
||
|
||
const distance =
|
||
direction === "rtl"
|
||
? event.clientX - rect.left
|
||
: rect.right - event.clientX;
|
||
return distance + (windowScrollbarWidth ?? 0);
|
||
},
|
||
[direction, windowScrollbarWidth]
|
||
);
|
||
|
||
const handleResize = React.useCallback(
|
||
(width: number) => ui.set({ sidebarRightWidth: width }),
|
||
[ui]
|
||
);
|
||
|
||
const handleResizeEnd = React.useCallback(() => {
|
||
if (document.activeElement instanceof HTMLElement) {
|
||
document.activeElement.blur();
|
||
}
|
||
|
||
// Settle within the bounds if released while stretched beyond them.
|
||
const settled = Math.max(
|
||
Math.min(ui.sidebarRightWidth, maxWidth),
|
||
minWidth
|
||
);
|
||
if (settled !== ui.sidebarRightWidth) {
|
||
ui.set({ sidebarRightWidth: settled });
|
||
}
|
||
}, [ui, minWidth, maxWidth]);
|
||
|
||
const { isResizing, startResize } = useResizeHandle({
|
||
measure,
|
||
onResize: handleResize,
|
||
onResizeEnd: handleResizeEnd,
|
||
min: minWidth,
|
||
max: maxWidth,
|
||
});
|
||
|
||
const handleReset = React.useCallback(() => {
|
||
ui.set({ sidebarRightWidth: theme.sidebarRightWidth });
|
||
}, [ui, theme.sidebarRightWidth]);
|
||
|
||
// Resizing tracks the pointer exactly, otherwise width changes spring – which also animates the
|
||
// snap back to the maximum or minimum when released from a stretched position.
|
||
const transition: Transition = isResizing
|
||
? { duration: 0 }
|
||
: {
|
||
type: "spring",
|
||
bounce: 0.2,
|
||
duration: sidebarAppearDuration / 1000,
|
||
};
|
||
|
||
// Growing the width reflows the content beside the sidebar on every frame. When the editor
|
||
// width changes at the final sidebar width, reserve it at once and slide the panel in instead.
|
||
React.useLayoutEffect(() => {
|
||
const sidebar = sidebarRef.current;
|
||
if (skipInitialAnimation || !sidebar) {
|
||
return;
|
||
}
|
||
|
||
const editor = sidebar.parentElement?.querySelector(
|
||
'[role="main"] .ProseMirror'
|
||
);
|
||
// Measure from a closed sidebar each time, as the effect can run again after it reserved the width.
|
||
sidebar.style.width = "0px";
|
||
const before = editor?.getBoundingClientRect().width;
|
||
sidebar.style.width = `${ui.sidebarRightWidth}px`;
|
||
if (editor && editor.getBoundingClientRect().width === before) {
|
||
sidebar.style.width = `${width.get()}px`;
|
||
return;
|
||
}
|
||
|
||
width.jump(ui.sidebarRightWidth);
|
||
x.jump(direction === "rtl" ? -ui.sidebarRightWidth : ui.sidebarRightWidth);
|
||
const slide = animate(x, 0, slideTransition);
|
||
return () => slide.stop();
|
||
// Only decided when the sidebar opens.
|
||
// oxlint-disable-next-line react-hooks/exhaustive-deps
|
||
}, []);
|
||
|
||
const animationProps = {
|
||
initial: skipInitialAnimation ? false : { opacity: 0.9 },
|
||
animate: {
|
||
transition,
|
||
width: ui.sidebarRightWidth,
|
||
opacity: 1,
|
||
},
|
||
exit: {
|
||
width: 0,
|
||
opacity: 0,
|
||
},
|
||
};
|
||
|
||
// The inner element is positioned out of flow, so it must follow the same width to stay in step
|
||
// with the container it visually fills.
|
||
const positionAnimationProps = {
|
||
initial: false,
|
||
animate: {
|
||
transition,
|
||
width: ui.sidebarRightWidth - (windowScrollbarWidth ?? 0),
|
||
},
|
||
};
|
||
|
||
return (
|
||
<Sidebar
|
||
ref={sidebarRef}
|
||
style={{ width }}
|
||
{...animationProps}
|
||
$border={border}
|
||
className={className}
|
||
role="complementary"
|
||
aria-label="Aside"
|
||
>
|
||
<Position ref={positionRef} style={{ x }} {...positionAnimationProps}>
|
||
<ErrorBoundary>{children}</ErrorBoundary>
|
||
<ResizeBorder
|
||
onMouseDown={startResize}
|
||
onDoubleClick={handleReset}
|
||
dir="right"
|
||
/>
|
||
</Position>
|
||
</Sidebar>
|
||
);
|
||
}
|
||
|
||
const Position = styled(m.div)`
|
||
display: flex;
|
||
flex-direction: column;
|
||
position: fixed;
|
||
top: 0;
|
||
bottom: 0;
|
||
max-width: 80%;
|
||
border-inline-start: 1px solid ${s("divider")};
|
||
transition: border-inline-start 100ms ease-in-out;
|
||
`;
|
||
|
||
const Sidebar = styled(m.div)<{
|
||
$border?: boolean;
|
||
}>`
|
||
display: block;
|
||
flex-shrink: 0;
|
||
background: ${s("background")};
|
||
max-width: 80%;
|
||
z-index: ${depths.sidebar};
|
||
|
||
${breakpoint("mobile", "tablet")`
|
||
display: flex;
|
||
position: absolute;
|
||
top: 0;
|
||
inset-inline-end: 0;
|
||
bottom: 0;
|
||
z-index: ${depths.mobileSidebar};
|
||
`}
|
||
|
||
${breakpoint("tablet")`
|
||
position: relative;
|
||
`}
|
||
`;
|
||
|
||
export default observer(Aside);
|