Skip to content
157 changes: 125 additions & 32 deletions pages/list/sortable-sticky-drawer-autoscroll.page.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import React, { useContext, useState } from 'react';
import React, { useContext, useEffect, useState } from 'react';

import { Box, List, SegmentedControl } from '~components';
import { Box, Input, List, SegmentedControl, SpaceBetween } from '~components';

import AppContext, { AppContextType } from '../app/app-context';
import { SimplePage } from '../app/templates';

type Layout = 'drawer-scroller' | 'drawer' | 'page';
type Layout =
| 'drawer-scroller'
| 'drawer'
| 'drawer-nested'
| 'container'
| 'container-x'
| 'page'
| 'body-viewport'
| 'body-scroller';

type PageContext = React.Context<AppContextType<{ layout: Layout }>>;
type PageContext = React.Context<AppContextType<{ layout: Layout; itemCount: string }>>;

interface Item {
id: string;
content: string;
}

const initialItems: Item[] = Array.from({ length: 30 }, (_, index) => ({
id: `item-${index + 1}`,
content: `Item ${index + 1}`,
}));

const outerHeight = 600;
const innerHeight = 300;

function createItems(count: number): Item[] {
return Array.from({ length: count }, (_, index) => ({ id: `item-${index + 1}`, content: `Item ${index + 1}` }));
}

function SortableList() {
const [items, setItems] = useState<readonly Item[]>(initialItems);
function SortableList({ itemCount }: { itemCount: number }) {
const [items, setItems] = useState<readonly Item[]>(() => createItems(itemCount));
return (
<List
ariaLabel="Sortable list"
Expand All @@ -36,15 +44,24 @@ function SortableList() {
);
}

function StickyDrawerLayout({ ownScroller }: { ownScroller: boolean }) {
function FillerRows({ count, prefix }: { count: number; prefix: string }) {
return (
<>
{Array.from({ length: count }, (_, index) => (
<Box key={index} padding="xxs">
{prefix} row {index + 1}
</Box>
))}
</>
);
}

function StickyDrawerLayout({ layout, itemCount }: { layout: Layout; itemCount: number }) {
const ownScroller = layout !== 'drawer';
return (
<div data-testid="outer-scroller" style={{ blockSize: outerHeight, overflow: 'auto', display: 'flex' }}>
<div style={{ flex: 1 }}>
{Array.from({ length: 150 }, (_, index) => (
<Box key={index} padding="xxs">
Main content row {index + 1}
</Box>
))}
<FillerRows count={150} prefix="Main content" />
</div>
<aside
data-testid="sticky-drawer"
Expand All @@ -61,41 +78,117 @@ function StickyDrawerLayout({ ownScroller }: { ownScroller: boolean }) {
<Box variant="h2" padding="s">
Drawer
</Box>
{ownScroller ? (
{layout === 'drawer-nested' ? (
<div data-testid="drawer-list-scroller" style={{ flex: 1, overflow: 'auto' }}>
<SortableList />
<FillerRows count={10} prefix="Drawer content" />
<div data-testid="inner-scroller" style={{ blockSize: innerHeight, overflowY: 'auto' }}>
<SortableList itemCount={itemCount} />
</div>
<FillerRows count={30} prefix="Drawer content" />
</div>
) : ownScroller ? (
<div data-testid="drawer-list-scroller" style={{ flex: 1, overflow: 'auto' }}>
<SortableList itemCount={itemCount} />
</div>
) : (
<SortableList />
<SortableList itemCount={itemCount} />
)}
</aside>
</div>
);
}

function ContainerLayout({ layout, itemCount }: { layout: Layout; itemCount: number }) {
const style: React.CSSProperties =
layout === 'container-x'
? { blockSize: innerHeight, overflowX: 'auto', overflowY: 'hidden' }
: { blockSize: innerHeight, overflowY: 'auto' };
return (
<div>
<FillerRows count={20} prefix="Page content" />
<div data-testid="inner-scroller" style={{ ...style, border: '1px solid gray' }}>
<SortableList itemCount={itemCount} />
</div>
<FillerRows count={60} prefix="Page content" />
</div>
);
}

// body-viewport: body has overflow-y: auto while html keeps overflow: visible, so the body's overflow applies to the
// viewport and the document scroller scrolls. body-scroller: html clips, so body is an independent scroll container.
function BodyOverflowLayout({ layout, itemCount }: { layout: Layout; itemCount: number }) {
useEffect(() => {
const html = document.documentElement;
const body = document.body;
const previous = { htmlOverflow: html.style.overflow, htmlHeight: html.style.blockSize, body: body.style.cssText };
body.style.overflowY = 'auto';
if (layout === 'body-scroller') {
html.style.overflow = 'hidden';
html.style.blockSize = '100%';
body.style.blockSize = '100%';
}
return () => {
html.style.overflow = previous.htmlOverflow;
html.style.blockSize = previous.htmlHeight;
body.style.cssText = previous.body;
};
}, [layout]);
return (
<div>
<FillerRows count={20} prefix="Page content" />
<SortableList itemCount={itemCount} />
<FillerRows count={60} prefix="Page content" />
</div>
);
}

export default function SortableListAutoScrollPage() {
const {
urlParams: { layout = 'drawer-scroller' },
urlParams: { layout = 'drawer-scroller', itemCount = '30' },
setUrlParams,
} = useContext(AppContext as PageContext);
const parsedItemCount = Math.max(1, parseInt(itemCount, 10) || 30);
const content =
layout === 'body-viewport' || layout === 'body-scroller' ? (
<BodyOverflowLayout layout={layout} itemCount={parsedItemCount} />
) : layout === 'page' ? (
<SortableList itemCount={parsedItemCount} />
) : layout === 'container' || layout === 'container-x' ? (
<ContainerLayout layout={layout} itemCount={parsedItemCount} />
) : (
<StickyDrawerLayout layout={layout} itemCount={parsedItemCount} />
);
return (
<SimplePage
title="Sortable list auto-scroll"
screenshotArea={{}}
settings={
<SegmentedControl
label="Layout"
selectedId={layout}
onChange={({ detail }) => setUrlParams({ layout: detail.selectedId as Layout })}
options={[
{ id: 'drawer-scroller', text: 'Sticky drawer with own scroller' },
{ id: 'drawer', text: 'Sticky drawer without own scroller' },
{ id: 'page', text: 'Page flow' },
]}
/>
<SpaceBetween size="s" direction="horizontal">
<SegmentedControl
label="Layout"
selectedId={layout}
onChange={({ detail }) => setUrlParams({ layout: detail.selectedId as Layout })}
options={[
{ id: 'drawer-scroller', text: 'Sticky drawer with own scroller' },
{ id: 'drawer', text: 'Sticky drawer without own scroller' },
{ id: 'drawer-nested', text: 'Nested scroller in sticky drawer' },
{ id: 'container', text: 'Fixed-height container' },
{ id: 'container-x', text: 'Horizontal-only container' },
{ id: 'page', text: 'Page flow' },
{ id: 'body-viewport', text: 'Body overflow on viewport' },
{ id: 'body-scroller', text: 'Body as own scroller' },
]}
/>
<Input
type="number"
ariaLabel="Item count"
value={itemCount}
onChange={({ detail }) => setUrlParams({ itemCount: detail.value })}
/>
</SpaceBetween>
}
>
{layout === 'page' ? <SortableList /> : <StickyDrawerLayout ownScroller={layout === 'drawer-scroller'} />}
<div key={`${layout}-${parsedItemCount}`}>{content}</div>
</SimplePage>
);
}
6 changes: 4 additions & 2 deletions src/internal/components/sortable-area/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ export default function SortableArea<Item>({

// dnd-kit auto-scrolls every scrollable ancestor up to the first fixed-position element, outermost first by default
// (https://github.com/clauderic/dnd-kit/issues/1893). Restrict it to the sortable area's nearest scroll container so
// that dragging inside e.g. a sticky drawer does not scroll unrelated outer containers. Without a scrollable ancestor,
// that dragging inside e.g. a sticky drawer does not scroll unrelated outer containers. The boundary is the nearest
// ancestor with a scrolling overflow-y even if its content does not currently overflow: such a container then simply
// has nothing to auto-scroll, rather than letting the outer containers scroll instead. Without such an ancestor,
// all containers (including the window) remain eligible.
function useScopedAutoScroll() {
const boundaryRef = useRef<HTMLElement | null>(null);
Expand All @@ -136,7 +138,7 @@ function useScopedAutoScroll() {
while (element && !isHTMLElement(element)) {
element = element.parentElement;
}
boundaryRef.current = element ? (getFirstScrollableParent(element) ?? null) : null;
boundaryRef.current = element ? (getFirstScrollableParent(element, { requireOverflow: false }) ?? null) : null;
};
return { autoScroll, setAutoScrollBoundary };
}
Expand Down
Loading
Loading