Files
outline/app/components/Sidebar/Aside.tsx
T
Tom Moor d57db81c6b feat: Split view (#13044)
Adds the ability to open a second route alongside the current one:

- The secondary route is stored in a `split` query string parameter so
  that reloading the page hydrates both panes.
- The secondary pane runs in its own in-memory router kept in sync with
  the query parameter, so links followed inside the pane navigate the
  pane rather than the primary route.
- Navigation while a split is open preserves the split, is directed to
  the focused pane, and routes that cannot render in a pane (such as
  settings) close the split.
- The focused pane is shown with an accent outline, updated as panes are
  clicked or focused, and keyboard shortcuts only apply to the focused
  pane.
- "Open in split view" added to document and collection menus and the
  command bar.
2026-07-19 11:57:33 -04:00

172 lines
4.7 KiB
TypeScript

import { m } from "framer-motion";
import { observer } from "mobx-react";
import * as React from "react";
import styled, { useTheme } from "styled-components";
import breakpoint from "styled-components-breakpoint";
import { depths, s } from "@shared/styles";
import ErrorBoundary from "~/components/ErrorBoundary";
import Flex from "~/components/Flex";
import ResizeBorder from "~/components/Sidebar/components/ResizeBorder";
import useStores from "~/hooks/useStores";
import useWindowScrollbarWidth from "~/hooks/useWindowScrollbarWidth";
import { sidebarAppearDuration } from "~/styles/animations";
import { useDirection } from "@radix-ui/react-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;
}
function Aside({ children, border, className, skipInitialAnimation }: Props) {
const theme = useTheme();
const { ui } = useStores();
const positionRef = React.useRef<HTMLDivElement>(null);
const [isResizing, setResizing] = React.useState(false);
const maxWidth = theme.sidebarMaxWidth;
const minWidth = theme.sidebarMinWidth + 16; // padding
const windowScrollbarWidth = useWindowScrollbarWidth();
const direction = useDirection();
const handleDrag = React.useCallback(
(event: MouseEvent) => {
// suppresses text selection
event.preventDefault();
// 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;
}
const distance =
direction === "rtl"
? event.clientX - rect.left
: rect.right - event.clientX;
const width = Math.max(
Math.min(distance + (windowScrollbarWidth ?? 0), maxWidth),
minWidth
);
ui.set({ sidebarRightWidth: width });
},
[minWidth, maxWidth, direction, ui, windowScrollbarWidth]
);
const handleReset = React.useCallback(() => {
ui.set({ sidebarRightWidth: theme.sidebarRightWidth });
}, [ui, theme.sidebarRightWidth]);
const handleStopDrag = React.useCallback(() => {
setResizing(false);
if (document.activeElement) {
// @ts-expect-error ts-migrate(2339) FIXME: Property 'blur' does not exist on type 'Element'.
document.activeElement.blur();
}
}, []);
const handleMouseDown = React.useCallback((event) => {
event.preventDefault();
setResizing(true);
}, []);
React.useEffect(() => {
if (isResizing) {
document.addEventListener("mousemove", handleDrag);
document.addEventListener("mouseup", handleStopDrag);
}
return () => {
document.removeEventListener("mousemove", handleDrag);
document.removeEventListener("mouseup", handleStopDrag);
};
}, [isResizing, handleDrag, handleStopDrag]);
const style = React.useMemo(
() => ({
width: windowScrollbarWidth
? `${ui.sidebarRightWidth - windowScrollbarWidth}px`
: `${ui.sidebarRightWidth}px`,
}),
[ui.sidebarRightWidth, windowScrollbarWidth]
);
const animationProps = {
initial: skipInitialAnimation
? false
: {
width: 0,
opacity: 0.9,
},
animate: {
transition: isResizing
? { duration: 0 }
: {
type: "spring",
bounce: 0.2,
duration: sidebarAppearDuration / 1000,
},
width: ui.sidebarRightWidth,
opacity: 1,
},
exit: {
width: 0,
opacity: 0,
},
};
return (
<Sidebar
{...animationProps}
$border={border}
className={className}
role="complementary"
aria-label="Aside"
>
<Position ref={positionRef} style={style} column>
<ErrorBoundary>{children}</ErrorBoundary>
<ResizeBorder
onMouseDown={handleMouseDown}
onDoubleClick={handleReset}
dir="right"
/>
</Position>
</Sidebar>
);
}
const Position = styled(Flex)`
position: fixed;
top: 0;
bottom: 0;
max-width: 80%;
`;
const Sidebar = styled(m.div)<{
$border?: boolean;
}>`
display: block;
flex-shrink: 0;
background: ${s("background")};
max-width: 80%;
border-inline-start: 1px solid ${s("divider")};
transition: border-inline-start 100ms ease-in-out;
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);