Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

feat(design): add `Scrollbar` component (#484)

白熱926e33e9a29b
diff --git a/packages/design/src/components/design-token/Palette.tsx b/packages/design/src/components/design-token/Palette.tsx
index 7508a57017..f49b6beeb3 100644
--- a/packages/design/src/components/design-token/Palette.tsx
+++ b/packages/design/src/components/design-token/Palette.tsx
@@ -61,6 +61,10 @@ export function Palette(props: IProps) {
title: 'Border Color',
colors: ['borderColor'],
},
+ {
+ title: 'Scrollbar Color',
+ colors: ['scrollbarColor', 'scrollbarColorHover', 'scrollbarColorActive'],
+ },
{
title: 'Border Radius',
colors: ['borderRadiusBase', 'borderRadiusLg', 'borderRadiusXl'],
diff --git a/packages/design/src/components/scrollbar/Scrollbar.stories.tsx b/packages/design/src/components/scrollbar/Scrollbar.stories.tsx
new file mode 100644
index 0000000000..e9401528a3
--- /dev/null
+++ b/packages/design/src/components/scrollbar/Scrollbar.stories.tsx
@@ -0,0 +1,185 @@
+import type { Meta, StoryObj } from '@storybook/react';
+
+import { Scrollbar } from './Scrollbar';
+
+const meta: Meta<typeof Scrollbar> = {
+ title: 'Components / Scrollbar',
+ component: Scrollbar,
+ parameters: {
+ layout: 'centered',
+ },
+ tags: ['autodocs'],
+};
+
+export default meta;
+
+export const ScrollbarBasic: StoryObj = {
+ render() {
+ return (
+ <section style={{ display: 'flex', gap: '24px' }}>
+ <section style={{ height: '320px' }}>
+ <Scrollbar>
+ top
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ bottom
+ </Scrollbar>
+ </section>
+
+ <section style={{ height: '30vh' }}>
+ <Scrollbar>
+ top
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ xxxx
+ <br />
+ bottom
+ </Scrollbar>
+ </section>
+
+ <section style={{ height: '100px' }}>
+ <Scrollbar>
+ <div>foo</div>
+ <div>foo</div>
+ </Scrollbar>
+ </section>
+ </section>
+ );
+ },
+};
diff --git a/packages/design/src/components/scrollbar/Scrollbar.tsx b/packages/design/src/components/scrollbar/Scrollbar.tsx
new file mode 100644
index 0000000000..211a48301b
--- /dev/null
+++ b/packages/design/src/components/scrollbar/Scrollbar.tsx
@@ -0,0 +1,133 @@
+import React, { useCallback, useEffect, useRef, useState } from 'react';
+
+import styles from './index.module.less';
+
+export interface IScrollbarProps {
+ children?: React.ReactNode;
+}
+
+/**
+ * Scrollbar Component
+ */
+export function Scrollbar(props: IScrollbarProps) {
+ const { children } = props;
+
+ const [isDragging, setIsDragging] = useState(false);
+ const [initialScrollTop, setInitialScrollTop] = useState(0);
+ const [scrollPositionY, setScrollPositionY] = useState(0);
+
+ const handleThumbMousedown = useCallback((e: React.MouseEvent | MouseEvent) => {
+ e.preventDefault();
+ e.stopPropagation();
+
+ setIsDragging(true);
+
+ setScrollPositionY(e.clientY);
+ setInitialScrollTop(contentRef.current!.scrollTop);
+ }, []);
+
+ const handleThumbMouseup = useCallback(
+ (e: MouseEvent) => {
+ e.preventDefault();
+ e.stopPropagation();
+
+ if (isDragging) {
+ setIsDragging(false);
+ }
+ },
+ [isDragging]
+ );
+
+ const handleThumbMousemove = useCallback(
+ (e: MouseEvent) => {
+ e.preventDefault();
+ e.stopPropagation();
+ if (isDragging) {
+ const { scrollHeight: contentScrollHeight, offsetHeight: contentOffsetHeight } = contentRef.current!;
+ const thumbHeight = contentOffsetHeight * (contentOffsetHeight / contentScrollHeight);
+ const deltaY = (e.clientY - scrollPositionY) * (contentOffsetHeight / thumbHeight);
+ const scrollTop = Math.min(initialScrollTop + deltaY, contentScrollHeight - contentOffsetHeight);
+
+ contentRef.current!.scrollTop = scrollTop;
+ }
+ },
+ [isDragging]
+ );
+
+ // Listen for mouse events to handle scrolling by dragging the thumb
+ useEffect(() => {
+ document.addEventListener('mousemove', handleThumbMousemove);
+ document.addEventListener('mouseup', handleThumbMouseup);
+ document.addEventListener('mouseleave', handleThumbMouseup);
+ return () => {
+ document.removeEventListener('mousemove', handleThumbMousemove);
+ document.removeEventListener('mouseup', handleThumbMouseup);
+ document.removeEventListener('mouseleave', handleThumbMouseup);
+ };
+ }, [handleThumbMousemove, handleThumbMouseup]);
+
+ const containerRef = useRef<HTMLDivElement>(null);
+ const contentRef = useRef<HTMLDivElement>(null);
+
+ const [thumbHeight, setThumbHeight] = useState(0);
+ const [thumbTop, setThumbTop] = useState(0);
+ useEffect(() => {
+ const observer = new ResizeObserver(() => {
+ let timer: number | undefined = requestIdleCallback(() => {
+ if (!timer) return;
+
+ const { height: containerHeight } = containerRef.current!.getBoundingClientRect();
+
+ const { scrollHeight } = contentRef.current!;
+ setThumbHeight((containerHeight / scrollHeight) * 100);
+
+ timer = undefined;
+ });
+ });
+
+ observer.observe(document.body);
+
+ // handle scroll event
+ contentRef.current!.addEventListener('scroll', (e) => {
+ const { scrollTop, scrollHeight } = e.target as HTMLDivElement;
+ const thumbTop = (scrollTop / scrollHeight) * 100;
+ setThumbTop(thumbTop);
+ });
+
+ return () => {
+ observer.unobserve(document.body);
+ };
+ }, []);
+
+ function handleJumpPosition(e: React.MouseEvent<HTMLDivElement, MouseEvent>) {
+ const { scrollHeight: contentScrollHeight, offsetHeight: contentOffsetHeight } = contentRef.current!;
+ const thumbHeight = contentOffsetHeight * (contentOffsetHeight / contentScrollHeight);
+ const deltaY = (e.clientY - scrollPositionY) * (contentOffsetHeight / thumbHeight);
+ const scrollTop = Math.min(initialScrollTop + deltaY, contentScrollHeight - contentOffsetHeight);
+
+ contentRef.current!.scrollTo({
+ top: scrollTop,
+ behavior: 'smooth',
+ });
+ }
+
+ return (
+ <div ref={containerRef} className={styles.scrollbarContainer}>
+ <div className={styles.scrollbarContent} ref={contentRef} {...props}>
+ {children}
+ </div>
+ {thumbHeight < 100 && thumbHeight > 0 && (
+ <div className={styles.scrollbarBar} role="scrollbar" onClick={handleJumpPosition}>
+ <div
+ className={styles.scrollbarBarThumb}
+ style={{
+ height: `${thumbHeight}%`,
+ top: `${thumbTop}%`,
+ }}
+ onMouseDown={handleThumbMousedown}
+ />
+ </div>
+ )}
+ </div>
+ );
+}
diff --git a/packages/design/src/components/scrollbar/__tests__/index.spec.tsx b/packages/design/src/components/scrollbar/__tests__/index.spec.tsx
new file mode 100644
index 0000000000..6d84d7e2bf
--- /dev/null
+++ b/packages/design/src/components/scrollbar/__tests__/index.spec.tsx
@@ -0,0 +1,23 @@
+import { render } from '@testing-library/react';
+import { describe, expect, test } from 'vitest';
+
+import { Scrollbar } from '../Scrollbar';
+
+describe('Scrollbar', () => {
+ test('should not render thumb', () => {
+ const { container } = render(
+ <section style={{ height: '100px' }}>
+ <Scrollbar>
+ <div>foo</div>
+ <div>foo</div>
+ </Scrollbar>
+ </section>
+ );
+
+ const scrollbar = container.querySelector('[role=scrollbar]');
+
+ console.log(scrollbar?.innerHTML);
+
+ expect(scrollbar).toBeNull();
+ });
+});
diff --git a/packages/design/src/components/scrollbar/index.module.less b/packages/design/src/components/scrollbar/index.module.less
new file mode 100644
index 0000000000..dbecb5eae2
--- /dev/null
+++ b/packages/design/src/components/scrollbar/index.module.less
@@ -0,0 +1,49 @@
+@scrollbar-width: 8px;
+
+.scrollbar {
+ &-container {
+ position: relative;
+ overflow: hidden;
+ height: 100%;
+ }
+
+ &-bar {
+ position: absolute;
+ top: 0;
+ right: 0;
+
+ width: @scrollbar-width;
+ height: 100%;
+
+ &:hover {
+ .scrollbar-bar-thumb:not(:active) {
+ background-color: rgb(var(--scrollbar-color-hover));
+ }
+ }
+
+ &-thumb {
+ position: absolute;
+ width: 100%;
+ background-color: rgb(var(--scrollbar-color));
+ border-radius: var(--border-radius-lg);
+
+ &:active {
+ background-color: rgb(var(--scrollbar-color-active));
+ }
+ }
+ }
+
+ &-content {
+ scrollbar-width: none;
+
+ overflow: auto;
+
+ box-sizing: border-box;
+ height: 100%;
+ padding-right: @scrollbar-width;
+
+ &::-webkit-scrollbar {
+ display: none;
+ }
+ }
+}
diff --git a/packages/design/src/components/scrollbar/index.ts b/packages/design/src/components/scrollbar/index.ts
new file mode 100644
index 0000000000..69c1b41f5d
--- /dev/null
+++ b/packages/design/src/components/scrollbar/index.ts
@@ -0,0 +1 @@
+export { type IScrollbarProps, Scrollbar } from './Scrollbar';
diff --git a/packages/design/src/index.ts b/packages/design/src/index.ts
index de4004433f..2400328ba4 100644
--- a/packages/design/src/index.ts
+++ b/packages/design/src/index.ts
@@ -14,6 +14,7 @@ export { Menu, MenuItem, MenuItemGroup, SubMenu } from './components/menu';
export { type IMessageMethodOptions, type IMessageProps, Message, MessageType } from './components/message';
export { type IRadioProps, Radio } from './components/radio';
export { type IRadioGroupProps, RadioGroup } from './components/radio-group';
+export { type IScrollbarProps, Scrollbar } from './components/scrollbar';
export { type ISelectProps, Select } from './components/select';
export { type ISelectListProps, SelectList } from './components/select-list';
export { type ISliderProps, Slider } from './components/slider';
diff --git a/packages/design/src/themes/base.less b/packages/design/src/themes/base.less
index 0100e22903..c23ed1e19c 100644
--- a/packages/design/src/themes/base.less
+++ b/packages/design/src/themes/base.less
@@ -377,6 +377,11 @@
// Border Color
@border-color: @grey-200;
+// Scrollbar Color
+@scrollbar-color: @grey-400;
+@scrollbar-color-hover: @grey-500;
+@scrollbar-color-active: @grey-600;
+
// Border Radius
@border-radius-base: 4px;
@border-radius-lg: 8px;
@@ -425,6 +430,11 @@
// Border Color
--border-color: @border-color;
+ // Scrollbar Color
+ --scrollbar-color: @scrollbar-color;
+ --scrollbar-color-hover: @scrollbar-color-hover;
+ --scrollbar-color-active: @scrollbar-color-active;
+
// Border Radius
--border-radius-base: @border-radius-base;
--border-radius-lg: @border-radius-lg;