Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: keep share page sidebar accessible

Richie McIlroy7826fa028b32
diff --git a/apps/web/__tests__/unit/share-stage-identity.test.ts b/apps/web/__tests__/unit/share-stage-identity.test.ts
index aa000167c..9bb74ad1e 100644
--- a/apps/web/__tests__/unit/share-stage-identity.test.ts
+++ b/apps/web/__tests__/unit/share-stage-identity.test.ts
@@ -77,6 +77,10 @@ vi.mock("@/actions/videos/get-status", () => ({
getVideoStatus: async () => ({}),
}));
+vi.mock("@/app/(org)/dashboard/_components/Navbar/server", () => ({
+ updateActiveOrganization: vi.fn(),
+}));
+
vi.mock("@/app/s/[videoId]/_components/CaptionContext", () => ({
CaptionProvider: ({ children }: { children?: ReactNode }) => children,
}));
@@ -91,7 +95,18 @@ vi.mock("@/app/s/[videoId]/_components/ShareVideo", async () => {
vi.mock("@/app/s/[videoId]/_components/Sidebar", async () => {
const { createElement } = await import("react");
- return { Sidebar: () => createElement("div", { "data-sidebar": "" }) };
+ return {
+ Sidebar: ({ onCollapse }: { onCollapse?: () => void }) =>
+ createElement(
+ "div",
+ { "data-sidebar": "" },
+ createElement(
+ "button",
+ { type: "button", onClick: onCollapse },
+ "Hide sidebar",
+ ),
+ ),
+ };
});
vi.mock("@/app/s/[videoId]/_components/Toolbar", async () => {
@@ -140,8 +155,8 @@ const createProps = (
});
const findButton = (container: HTMLElement, label: string) =>
- Array.from(container.querySelectorAll("button")).find(
- (button) => button.textContent === label,
+ Array.from(container.querySelectorAll("button")).find((button) =>
+ button.textContent?.endsWith(label),
);
/** The stage wrapper is three levels above the `<video>`: player -> inset box -> card -> stage. */
@@ -155,6 +170,7 @@ describe("Share view toggle", () => {
afterEach(() => {
document.body.replaceChildren();
+ window.localStorage.clear();
window.history.replaceState(window.history.state, "", "/s/video-id");
});
@@ -162,6 +178,98 @@ describe("Share view toggle", () => {
delete actEnvironment.IS_REACT_ACT_ENVIRONMENT;
});
+ it("keeps the sidebar open after mounting with an old collapsed preference", async () => {
+ window.localStorage.setItem("cap_share_rail_collapsed", "true");
+ const container = document.createElement("div");
+ document.body.append(container);
+ const root = createRoot(container);
+
+ await act(async () => {
+ root.render(createElement(Share, createProps()));
+ });
+
+ expect(container.querySelector("aside")?.classList.contains("lg:w-0")).toBe(
+ false,
+ );
+ expect(
+ container
+ .querySelector("aside > div")
+ ?.classList.contains("lg:invisible"),
+ ).toBe(false);
+ expect(findButton(container, "Show sidebar")).toBeUndefined();
+
+ await act(async () => {
+ root.unmount();
+ });
+ });
+
+ it("preserves the sidebar and player while collapsing, without persisting to another visit", async () => {
+ const container = document.createElement("div");
+ document.body.append(container);
+ let root = createRoot(container);
+
+ await act(async () => {
+ root.render(createElement(Share, createProps()));
+ });
+
+ const sidebar = container.querySelector("[data-sidebar]");
+ const video = container.querySelector("[data-share-video]");
+ expect(sidebar).not.toBeNull();
+ expect(video).not.toBeNull();
+
+ await act(async () => {
+ findButton(container, "Hide sidebar")?.click();
+ });
+
+ expect(container.querySelector("aside")?.classList.contains("lg:w-0")).toBe(
+ true,
+ );
+ expect(
+ container
+ .querySelector("aside > div")
+ ?.classList.contains("lg:invisible"),
+ ).toBe(true);
+ expect(container.querySelector("[data-sidebar]")).toBe(sidebar);
+ expect(container.querySelector("[data-share-video]")).toBe(video);
+ const reopen = findButton(container, "Show sidebar");
+ expect(reopen).toBeDefined();
+ expect(reopen?.getAttribute("aria-controls")).toBe(
+ container.querySelector("aside")?.id,
+ );
+ expect(reopen?.getAttribute("aria-expanded")).toBe("false");
+ expect(window.localStorage.getItem("cap_share_rail_collapsed")).toBeNull();
+
+ await act(async () => {
+ reopen?.click();
+ });
+
+ expect(container.querySelector("aside")?.classList.contains("lg:w-0")).toBe(
+ false,
+ );
+ expect(container.querySelector("[data-sidebar]")).toBe(sidebar);
+ expect(container.querySelector("[data-share-video]")).toBe(video);
+
+ await act(async () => {
+ findButton(container, "Hide sidebar")?.click();
+ });
+ await act(async () => {
+ root.unmount();
+ });
+ root = createRoot(container);
+ await act(async () => {
+ root.render(createElement(Share, createProps()));
+ });
+
+ expect(container.querySelector("aside")?.classList.contains("lg:w-0")).toBe(
+ false,
+ );
+ expect(findButton(container, "Show sidebar")).toBeUndefined();
+
+ await act(async () => {
+ root.unmount();
+ });
+ });
+
it("keeps the same stage and video element across view changes", async () => {
const container = document.createElement("div");
document.body.append(container);
diff --git a/apps/web/app/s/[videoId]/Share.tsx b/apps/web/app/s/[videoId]/Share.tsx
index 0de12a16a..c747064d8 100644
--- a/apps/web/app/s/[videoId]/Share.tsx
+++ b/apps/web/app/s/[videoId]/Share.tsx
@@ -15,6 +15,7 @@ import {
use,
useCallback,
useEffect,
+ useId,
useMemo,
useOptimistic,
useRef,
@@ -47,9 +48,6 @@ const TimelineView = dynamic(importTimelineView, { ssr: false });
// the summary into the initial HTML when the data is already there.
const SummaryChapters = dynamic(() => import("./_components/SummaryChapters"));
-/** Whether the viewer last left the comments rail collapsed. */
-const RAIL_COLLAPSED_KEY = "cap_share_rail_collapsed";
-
/**
* The interactive walkthrough behind the header's "How does this work?".
* Relative, so a custom domain or a self-hosted deployment links at its own
@@ -726,31 +724,12 @@ export const Share = ({
]);
const showRail = view === "classic" && !allSettingsDisabled;
+ const sidebarId = useId();
const [railCollapsed, setRailCollapsed] = useState(false);
- // Read after mount, not during render: the server has no idea what the
- // viewer collapsed last time, and guessing would hydrate the wrong width.
- useEffect(() => {
- try {
- setRailCollapsed(
- window.localStorage.getItem(RAIL_COLLAPSED_KEY) === "true",
- );
- } catch {
- /* private mode; the default (open) is fine */
- }
- }, []);
-
const toggleRail = useCallback(() => {
- // Persist outside the updater: React may invoke updater functions more
- // than once, and localStorage writes don't belong in render-adjacent code.
- const next = !railCollapsed;
- setRailCollapsed(next);
- try {
- window.localStorage.setItem(RAIL_COLLAPSED_KEY, String(next));
- } catch {
- /* not worth failing the toggle over */
- }
- }, [railCollapsed]);
+ setRailCollapsed((collapsed) => !collapsed);
+ }, []);
return (
<CaptionProvider
@@ -1034,7 +1013,12 @@ export const Share = ({
* absolute placement (desktop) keeps the toolbar pill
* centred rather than pushed aside.
*/}
- <div className="relative">
+ <div
+ className={clsx(
+ "relative",
+ showRail && railCollapsed && "lg:min-h-10 lg:px-40",
+ )}
+ >
{timelineAvailable && (
<div className="mb-3 flex justify-start lg:absolute lg:left-0 lg:top-1/2 lg:mb-0 lg:-translate-y-1/2">
<ShareViewToggle
@@ -1053,6 +1037,18 @@ export const Share = ({
canRecordMedia={canRecordMedia && !isScreenshot}
data={data}
/>
+ {showRail && railCollapsed && (
+ <button
+ type="button"
+ onClick={toggleRail}
+ aria-controls={sidebarId}
+ aria-expanded={false}
+ className="hidden absolute right-0 top-1/2 items-center justify-center gap-2 -translate-y-1/2 rounded-lg border border-gray-5 bg-white h-10 px-3 text-sm font-medium text-gray-10 shadow-sm transition-colors hover:text-gray-12 lg:flex"
+ >
+ <ChevronGlyph direction="left" />
+ Show sidebar
+ </button>
+ )}
</div>
</motion.div>
) : (
@@ -1157,6 +1153,7 @@ export const Share = ({
*/}
{showRail && (
<aside
+ id={sidebarId}
className={clsx(
"shrink-0 px-4 pb-8 lg:p-0 lg:h-full lg:border-l lg:border-gray-5 lg:bg-white lg:overflow-hidden",
reduceMotion
@@ -1165,10 +1162,6 @@ export const Share = ({
railCollapsed ? "lg:w-0" : "lg:w-[22rem] xl:w-[24rem]",
)}
>
- {/* Hidden rather than merely clipped while collapsed, so the
- panel's inputs leave the tab order. Scoped to lg: the
- stored preference is a desktop one and the phone layout
- shows the panel regardless. */}
<div
className={clsx(
"lg:h-full lg:w-[22rem] xl:w-[24rem]",
@@ -1176,6 +1169,7 @@ export const Share = ({
)}
>
<Sidebar
+ sidebarId={sidebarId}
data={sidebarData}
videoSettings={videoSettings}
commentsData={commentsData}
@@ -1197,20 +1191,6 @@ export const Share = ({
</div>
</aside>
)}
-
- {/* Only handle back to the panel once it's away. Sits on the edge
- it collapsed into, so the gesture reverses itself. */}
- {showRail && railCollapsed && (
- <button
- type="button"
- onClick={toggleRail}
- aria-label="Show comments"
- title="Show comments"
- className="hidden fixed right-0 top-1/2 z-30 items-center justify-center -translate-y-1/2 rounded-l-lg border border-r-0 border-gray-5 bg-white h-16 w-6 text-gray-10 shadow-sm transition-colors hover:text-gray-12 lg:flex"
- >
- <ChevronGlyph direction="left" />
- </button>
- )}
</div>
</PlaybackProvider>
</CaptionProvider>
diff --git a/apps/web/app/s/[videoId]/_components/Sidebar.tsx b/apps/web/app/s/[videoId]/_components/Sidebar.tsx
index 8dab24e07..51731fc3d 100644
--- a/apps/web/app/s/[videoId]/_components/Sidebar.tsx
+++ b/apps/web/app/s/[videoId]/_components/Sidebar.tsx
@@ -44,6 +44,7 @@ type AiGenerationStatus =
| "SKIPPED";
interface SidebarProps {
+ sidebarId?: string;
data: VideoData;
commentsData: CommentType[];
optimisticComments: CommentType[];
@@ -99,6 +100,7 @@ const tabTransition = {
export const Sidebar = forwardRef<{ scrollToBottom: () => void }, SidebarProps>(
(
{
+ sidebarId,
data,
commentsData,
setCommentsData,
@@ -300,8 +302,10 @@ export const Sidebar = forwardRef<{ scrollToBottom: () => void }, SidebarProps>(
onClick={() => {
if (canLeaveSummary()) onCollapse();
}}
- aria-label="Hide comments"
- title="Hide comments"
+ aria-label="Hide sidebar"
+ title="Hide sidebar"
+ aria-controls={sidebarId}
+ aria-expanded={true}
className="hidden shrink-0 items-center justify-center px-3 text-gray-9 transition-colors hover:bg-gray-1 hover:text-gray-12 lg:flex"
>
<svg
@@ -309,7 +313,7 @@ export const Sidebar = forwardRef<{ scrollToBottom: () => void }, SidebarProps>(
className="size-4 fill-current"
aria-hidden
>
- <title>Hide comments</title>
+ <title>Hide sidebar</title>
<path d="M5.7 3.3a.6.6 0 0 0 0 .85L9.55 8 5.7 11.85a.6.6 0 1 0 .85.85l4.27-4.27a.6.6 0 0 0 0-.86L6.55 3.3a.6.6 0 0 0-.85 0Z" />
</svg>
</button>