Files
outline/app/components/Sidebar/Aside.tsx
T
Tom MoorandClaude Opus 4.7 49d5052a51 feat: RTL layout (#12107)
* First pass

* Remove prop drilling, fix comment layout

* Revert dev:watch to use dev:backend

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-04-18 15:12:57 -04:00

159 lines
4.2 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 [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();
const distance =
direction === "rtl" ? event.pageX : window.innerWidth - event.pageX;
const width = Math.max(Math.min(distance, maxWidth), minWidth);
ui.set({ sidebarRightWidth: width });
},
[minWidth, maxWidth, direction, ui]
);
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 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);