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(base-ui): new `Slider` component (#277)

白熱3d879c4eeafc
diff --git a/packages/base-ui/src/Components/Button/Button.tsx b/packages/base-ui/src/Components/Button/Button.tsx
index 799cc6278d..1b7dada9b0 100644
--- a/packages/base-ui/src/Components/Button/Button.tsx
+++ b/packages/base-ui/src/Components/Button/Button.tsx
@@ -4,7 +4,7 @@ import { LoadingIcon } from '../Icon';
import styles from './Style/index.module.less';
// Components interface
-export type ButtonType = 'default' | 'primary';
+export type ButtonType = 'default' | 'primary' | 'text';
export type ButtonShape = 'circle' | 'round';
export type SizeType = 'small' | 'middle' | 'large';
export type ButtonHTMLType = 'submit' | 'reset' | 'button';
diff --git a/packages/base-ui/src/Components/Button/Style/mixin.less b/packages/base-ui/src/Components/Button/Style/mixin.less
index ab64489af0..e34b430f0b 100644
--- a/packages/base-ui/src/Components/Button/Style/mixin.less
+++ b/packages/base-ui/src/Components/Button/Style/mixin.less
@@ -55,6 +55,7 @@
.btn-text() {
.button-color(var(--text-color), transparent, transparent);
+ border-radius: @border-radius-base;
box-shadow: none;
&:hover {
diff --git a/packages/base-ui/src/Components/Dropdown/Dropdown2.tsx b/packages/base-ui/src/Components/Dropdown/Dropdown2.tsx
index 39da96e886..72a34f95be 100644
--- a/packages/base-ui/src/Components/Dropdown/Dropdown2.tsx
+++ b/packages/base-ui/src/Components/Dropdown/Dropdown2.tsx
@@ -1,15 +1,16 @@
import RcDropdown from 'rc-dropdown';
+import Placements from 'rc-dropdown/lib/placements';
import styles from './index.module.less';
interface IDropdownProps {
+ placement?: keyof typeof Placements;
children: React.ReactElement;
overlay: React.ReactElement;
}
export function Dropwdown2(props: IDropdownProps) {
- const { children, overlay } = props;
- console.log(styles.dropdown2);
+ const { placement, children, overlay } = props;
return (
<RcDropdown
@@ -18,6 +19,7 @@ export function Dropwdown2(props: IDropdownProps) {
overlayClassName={styles.dropdown2}
trigger={['click']}
animation="slide-up"
+ placement={placement}
overlay={overlay}
>
{children}
diff --git a/packages/base-ui/src/Components/Dropdown/index.module.less b/packages/base-ui/src/Components/Dropdown/index.module.less
index a2fd51c333..83d2c0f743 100644
--- a/packages/base-ui/src/Components/Dropdown/index.module.less
+++ b/packages/base-ui/src/Components/Dropdown/index.module.less
@@ -1,5 +1,192 @@
@import '../../Basics/CSS/Skin/default.module.less';
+@dropdown-arrow-width: 8px;
+@dropdown-distance: @dropdown-arrow-width + 4;
+@dropdown-arrow-color: #373737;
+@dropdown-overlay-shadow: 0 1px 5px #ccc;
+
+.dropdown2 {
+ position: absolute;
+ left: -9999px;
+ top: -9999px;
+ z-index: 1070;
+ display: block;
+
+ &-hidden {
+ display: none;
+ }
+
+ .effect() {
+ animation-duration: 0.3s;
+ animation-fill-mode: both;
+ transform-origin: 0 0;
+ display: block !important;
+ }
+
+ &-slide-up-enter,
+ &-slide-up-appear {
+ .effect();
+ opacity: 0;
+ animation-timing-function: cubic-bezier(0.08, 0.82, 0.17, 1);
+ animation-play-state: paused;
+ }
+
+ &-slide-up-leave {
+ .effect();
+ opacity: 1;
+ animation-timing-function: cubic-bezier(0.6, 0.04, 0.98, 0.34);
+ animation-play-state: paused;
+ }
+
+ &-slide-up-enter&-slide-up-enter-active&-placement-bottomLeft,
+ &-slide-up-appear&-slide-up-appear-active&-placement-bottomLeft,
+ &-slide-up-enter&-slide-up-enter-active&-placement-bottomCenter,
+ &-slide-up-appear&-slide-up-appear-active&-placement-bottomCenter,
+ &-slide-up-enter&-slide-up-enter-active&-placement-bottomRight,
+ &-slide-up-appear&-slide-up-appear-active&-placement-bottomRight {
+ animation-name: rcDropdownSlideUpIn;
+ animation-play-state: running;
+ }
+
+ &-slide-up-enter&-slide-up-enter-active&-placement-topLeft,
+ &-slide-up-appear&-slide-up-appear-active&-placement-topLeft,
+ &-slide-up-enter&-slide-up-enter-active&-placement-topCenter,
+ &-slide-up-appear&-slide-up-appear-active&-placement-topCenter,
+ &-slide-up-enter&-slide-up-enter-active&-placement-topRight,
+ &-slide-up-appear&-slide-up-appear-active&-placement-topRight {
+ animation-name: rcDropdownSlideDownIn;
+ animation-play-state: running;
+ }
+
+ &-slide-up-leave&-slide-up-leave-active&-placement-bottomLeft,
+ &-slide-up-leave&-slide-up-leave-active&-placement-bottomCenter,
+ &-slide-up-leave&-slide-up-leave-active&-placement-bottomRight {
+ animation-name: rcDropdownSlideUpOut;
+ animation-play-state: running;
+ }
+
+ &-slide-up-leave&-slide-up-leave-active&-placement-topLeft,
+ &-slide-up-leave&-slide-up-leave-active&-placement-topCenter,
+ &-slide-up-leave&-slide-up-leave-active&-placement-topRight {
+ animation-name: rcDropdownSlideDownOut;
+ animation-play-state: running;
+ }
+
+ @keyframes rcDropdownSlideUpIn {
+ 0% {
+ opacity: 0;
+ transform-origin: 0% 0%;
+ transform: scaleY(0);
+ }
+ 100% {
+ opacity: 1;
+ transform-origin: 0% 0%;
+ transform: scaleY(1);
+ }
+ }
+ @keyframes rcDropdownSlideUpOut {
+ 0% {
+ opacity: 1;
+ transform-origin: 0% 0%;
+ transform: scaleY(1);
+ }
+ 100% {
+ opacity: 0;
+ transform-origin: 0% 0%;
+ transform: scaleY(0);
+ }
+ }
+
+ @keyframes rcDropdownSlideDownIn {
+ 0% {
+ opacity: 0;
+ transform-origin: 0% 100%;
+ transform: scaleY(0);
+ }
+ 100% {
+ opacity: 1;
+ transform-origin: 0% 100%;
+ transform: scaleY(1);
+ }
+ }
+ @keyframes rcDropdownSlideDownOut {
+ 0% {
+ opacity: 1;
+ transform-origin: 0% 100%;
+ transform: scaleY(1);
+ }
+ 100% {
+ opacity: 0;
+ transform-origin: 0% 100%;
+ transform: scaleY(0);
+ }
+ }
+
+ // adjust padding
+ // adjust padding
+ &-show-arrow&-placement-top,
+ &-show-arrow&-placement-topLeft,
+ &-show-arrow&-placement-topRight {
+ padding-bottom: 6px;
+ }
+
+ &-show-arrow&-placement-bottom,
+ &-show-arrow&-placement-bottomLeft,
+ &-show-arrow&-placement-bottomRight {
+ padding-top: 6px;
+ }
+
+ // top-*
+ &-placement-top &-arrow,
+ &-placement-topLeft &-arrow,
+ &-placement-topRight &-arrow {
+ bottom: @dropdown-distance - @dropdown-arrow-width;
+ border-top-color: white;
+ }
+
+ &-placement-top &-arrow {
+ left: 50%;
+ }
+
+ &-placement-topLeft &-arrow {
+ left: 15%;
+ }
+
+ &-placement-topRight &-arrow {
+ right: 15%;
+ }
+
+ // bottom-*
+ &-placement-bottom &-arrow,
+ &-placement-bottomLeft &-arrow,
+ &-placement-bottomRight &-arrow {
+ top: @dropdown-distance - @dropdown-arrow-width;
+ border-bottom-color: white;
+ }
+
+ &-placement-bottom &-arrow {
+ left: 50%;
+ }
+
+ &-placement-bottomLeft &-arrow {
+ left: 15%;
+ }
+
+ &-placement-bottomRight &-arrow {
+ right: 15%;
+ }
+}
+
+// arrows
+.dropdown2-arrow {
+ position: absolute;
+ border-width: @dropdown-arrow-width / 2;
+ border-color: transparent;
+ box-shadow: @dropdown-overlay-shadow;
+ border-style: solid;
+ transform: rotate(45deg);
+}
+
.dropdown {
// padding: 4px 8px;
position: relative;
@@ -22,198 +209,3 @@
font-size: 10px;
}
}
-
-
-@import '../../Basics/CSS/Skin/default.module.less';
-
-@dropdown-arrow-width: 8px;
-@dropdown-distance: @dropdown-arrow-width + 4;
-@dropdown-arrow-color: #373737;
-@dropdown-overlay-shadow: 0 1px 5px #ccc;
-
-
-.dropdown2 {
- position: absolute;
- left: -9999px;
- top: -9999px;
- z-index: 1070;
- display: block;
- font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
- font-size: 12px;
- font-weight: normal;
- line-height: 1.5;
-
- &-hidden {
- display: none;
- }
-
- .effect() {
- animation-duration: 0.3s;
- animation-fill-mode: both;
- transform-origin: 0 0;
- display: block !important;
- }
-
- &-slide-up-enter,&-slide-up-appear {
- .effect();
- opacity: 0;
- animation-timing-function: cubic-bezier(0.08, 0.82, 0.17, 1);
- animation-play-state: paused;
- }
-
- &-slide-up-leave {
- .effect();
- opacity: 1;
- animation-timing-function: cubic-bezier(0.6, 0.04, 0.98, 0.34);
- animation-play-state: paused;
- }
-
- &-slide-up-enter&-slide-up-enter-active&-placement-bottomLeft,
- &-slide-up-appear&-slide-up-appear-active&-placement-bottomLeft,
- &-slide-up-enter&-slide-up-enter-active&-placement-bottomCenter,
- &-slide-up-appear&-slide-up-appear-active&-placement-bottomCenter,
- &-slide-up-enter&-slide-up-enter-active&-placement-bottomRight,
- &-slide-up-appear&-slide-up-appear-active&-placement-bottomRight {
- animation-name: rcDropdownSlideUpIn;
- animation-play-state: running;
- }
-
- &-slide-up-enter&-slide-up-enter-active&-placement-topLeft,
- &-slide-up-appear&-slide-up-appear-active&-placement-topLeft,
- &-slide-up-enter&-slide-up-enter-active&-placement-topCenter,
- &-slide-up-appear&-slide-up-appear-active&-placement-topCenter,
- &-slide-up-enter&-slide-up-enter-active&-placement-topRight,
- &-slide-up-appear&-slide-up-appear-active&-placement-topRight {
- animation-name: rcDropdownSlideDownIn;
- animation-play-state: running;
- }
-
- &-slide-up-leave&-slide-up-leave-active&-placement-bottomLeft,
- &-slide-up-leave&-slide-up-leave-active&-placement-bottomCenter,
- &-slide-up-leave&-slide-up-leave-active&-placement-bottomRight {
- animation-name: rcDropdownSlideUpOut;
- animation-play-state: running;
- }
-
- &-slide-up-leave&-slide-up-leave-active&-placement-topLeft,
- &-slide-up-leave&-slide-up-leave-active&-placement-topCenter,
- &-slide-up-leave&-slide-up-leave-active&-placement-topRight {
- animation-name: rcDropdownSlideDownOut;
- animation-play-state: running;
- }
-
- @keyframes rcDropdownSlideUpIn {
- 0% {
- opacity: 0;
- transform-origin: 0% 0%;
- transform: scaleY(0);
- }
- 100% {
- opacity: 1;
- transform-origin: 0% 0%;
- transform: scaleY(1);
- }
- }
- @keyframes rcDropdownSlideUpOut {
- 0% {
- opacity: 1;
- transform-origin: 0% 0%;
- transform: scaleY(1);
- }
- 100% {
- opacity: 0;
- transform-origin: 0% 0%;
- transform: scaleY(0);
- }
- }
-
- @keyframes rcDropdownSlideDownIn {
- 0% {
- opacity: 0;
- transform-origin: 0% 100%;
- transform: scaleY(0);
- }
- 100% {
- opacity: 1;
- transform-origin: 0% 100%;
- transform: scaleY(1);
- }
- }
- @keyframes rcDropdownSlideDownOut {
- 0% {
- opacity: 1;
- transform-origin: 0% 100%;
- transform: scaleY(1);
- }
- 100% {
- opacity: 0;
- transform-origin: 0% 100%;
- transform: scaleY(0);
- }
- }
-}
-
-// arrows
-.dropdown2-arrow {
- position: absolute;
- border-width: @dropdown-arrow-width / 2;
- border-color: transparent;
- box-shadow: @dropdown-overlay-shadow;
- border-style: solid;
- transform: rotate(45deg);
-}
-
-.dropdown2 {
- // adjust padding
- &-show-arrow&-placement-top,
- &-show-arrow&-placement-topLeft,
- &-show-arrow&-placement-topRight {
- padding-bottom: 6px;
- }
-
- &-show-arrow&-placement-bottom,
- &-show-arrow&-placement-bottomLeft,
- &-show-arrow&-placement-bottomRight {
- padding-top: 6px;
- }
-
- // top-*
- &-placement-top &-arrow,
- &-placement-topLeft &-arrow,
- &-placement-topRight &-arrow {
- bottom: @dropdown-distance - @dropdown-arrow-width;
- border-top-color: white;
- }
-
- &-placement-top &-arrow {
- left: 50%;
- }
-
- &-placement-topLeft &-arrow {
- left: 15%;
- }
-
- &-placement-topRight &-arrow {
- right: 15%;
- }
-
- // bottom-*
- &-placement-bottom &-arrow,
- &-placement-bottomLeft &-arrow,
- &-placement-bottomRight &-arrow {
- top: @dropdown-distance - @dropdown-arrow-width;
- border-bottom-color: white;
- }
-
- &-placement-bottom &-arrow {
- left: 50%;
- }
-
- &-placement-bottomLeft &-arrow {
- left: 15%;
- }
-
- &-placement-bottomRight &-arrow {
- right: 15%;
- }
-}
diff --git a/packages/base-ui/src/Components/Icon/Cell/index.tsx b/packages/base-ui/src/Components/Icon/Cell/index.tsx
index 097916220b..b09471c0ab 100644
--- a/packages/base-ui/src/Components/Icon/Cell/index.tsx
+++ b/packages/base-ui/src/Components/Icon/Cell/index.tsx
@@ -1,5 +1,5 @@
import {
- BinaryColorExample24,
+ BorderColor24,
DownBorde24,
InnerBorde24,
LeftBorde24,
@@ -14,7 +14,7 @@ import {
import { BaseIconProps } from '../AddIcon';
export const FillColorIcon = (props: BaseIconProps) => (
- <BinaryColorExample24 extend={{ colorChannel1: props.color || 'white' }} />
+ <BorderColor24 extend={{ colorChannel1: props.color || 'white' }} />
);
export const FullBorderIcon = (props: BaseIconProps) => <TableStyle24 />;
diff --git a/packages/base-ui/src/Components/Slider/Slider.stories.tsx b/packages/base-ui/src/Components/Slider/Slider.stories.tsx
index 4446b31221..ddf5ba7b1b 100644
--- a/packages/base-ui/src/Components/Slider/Slider.stories.tsx
+++ b/packages/base-ui/src/Components/Slider/Slider.stories.tsx
@@ -1,4 +1,5 @@
import type { Meta } from '@storybook/react';
+import { useState } from 'react';
import { Slider } from './Slider';
@@ -9,16 +10,18 @@ const meta: Meta<typeof Slider> = {
layout: 'centered',
},
tags: ['autodocs'],
- args: {
- min: 0,
- max: 100,
- step: 1,
- range: false,
- },
};
export default meta;
export const Playground = {
- args: {},
+ render() {
+ const [value, setValue] = useState(0);
+
+ function handleChange(changedValue: number) {
+ setValue(changedValue);
+ }
+
+ return <Slider value={value} shortcuts={[50, 75, 100, 125, 150, 175, 200, 400]} onChange={handleChange} />;
+ },
};
diff --git a/packages/base-ui/src/Components/Slider/Slider.tsx b/packages/base-ui/src/Components/Slider/Slider.tsx
index 38bb47f292..a5377ba2cf 100644
--- a/packages/base-ui/src/Components/Slider/Slider.tsx
+++ b/packages/base-ui/src/Components/Slider/Slider.tsx
@@ -1,13 +1,22 @@
-import React, { CSSProperties, useEffect, useRef, useState } from 'react';
+/* eslint-disable no-magic-numbers */
+import { useMemo, useRef } from 'react';
-import { randomId } from '../../Utils';
+import { joinClassNames } from '../../Utils';
+import { Button } from '../Button/Button';
+import { Dropwdown2 } from '../Dropdown/Dropdown2';
+import { Tooltip } from '../Tooltip/Tooltip';
import styles from './index.module.less';
-// Component Interface
+/**
+ * TODO:
+ * 1. Replace icons
+ * 2. Localization '恢复至 100%'
+ */
+
+interface IBaseSliderProps {
+ /** The value of slider. When range is false, use number, otherwise, use [number, number] */
+ value: number;
-// Base Slider Props
-interface BaseSliderProps {
- id?: string;
/**
* The minimum value the slider can slide to
* @default 0
@@ -15,194 +24,144 @@ interface BaseSliderProps {
min?: number;
/** The maximum value the slider can slide to
- * @default 100
+ * @default 400
*/
max?: number;
- /**
- * The granularity the slider can step through values. Must greater than 0, and be divided by (max - min) . When marks no null, step can be null
- * @default 1
+ /** The maximum value the slider can slide to
+ * @default 100
*/
- step?: number;
+ resetPoint?: number;
- /** slider class name */
- className?: string;
-
- /** slider style */
- style?: React.CSSProperties;
-
- /**
- * Dual thumb mode
- * @default false
- * */
- range?: boolean;
-
- /** The value of slider. When range is false, use number, otherwise, use [number, number] */
- value?: number | [number, number];
-
- /** (value) => void */
- onChange?: (e: Event) => void;
+ /** Shortcuts of slider */
+ shortcuts?: number[];
/** (value) => void */
- onClick?: (e: Event, value: number | string) => void;
+ onChange?: (value: number) => void;
}
/**
* Slider Component
*/
-export function Slider(props: BaseSliderProps) {
- const [valuePrev, setValuePrev] = useState<number>(0);
- const [valueNext, setValueNext] = useState<number>(0);
- const idPrevRef = useRef<string>(randomId('slider'));
- const idNextRef = useRef<string>(randomId('slider'));
- const sliderRef = useRef<HTMLDivElement | null>(null);
-
- const { min = 0, max = 100, step = 1, range = false, value, className = '' } = props;
-
- useEffect(() => {
- const diff = max - min;
- const slider = sliderRef.current;
-
- if (slider) {
- slider.style.cssText += `--${idPrevRef.current}: ${valuePrev}; --${idNextRef.current}: ${valueNext}`;
- const styleSheet = document.styleSheets[0];
-
- if (styleSheet) {
- styleSheet.insertRule(
- `.${styles.slider}::after {margin-left: calc((var(--${idNextRef.current}) - ${min})/${diff}*100%); width: calc((var(--${idPrevRef.current}) - var(--${idNextRef.current}))/${diff}*100%);}`,
- 0
- );
- styleSheet.insertRule(
- `.${styles.slider}::before {margin-left: calc((var(--${idPrevRef.current}) - ${min})/${diff}*100%);width: calc((var(--${idNextRef.current}) - var(--${idPrevRef.current}))/${diff}*100%);}`,
- 0
- );
- }
+export function Slider(props: IBaseSliderProps) {
+ const { value, min = 0, max = 400, resetPoint = 100, shortcuts, onChange } = props;
+
+ const sliderInnerRailRef = useRef<HTMLDivElement>(null);
+
+ function handleReset() {
+ onChange && onChange(resetPoint);
+ }
+
+ function handleStep(offset: number) {
+ onChange && onChange(value + offset);
+ }
+
+ const offset = useMemo(() => {
+ const breakpoint = [resetPoint, (max - resetPoint) / 2 + resetPoint, max];
+ if (value <= breakpoint[0]) {
+ return value * 0.5;
}
- // 初始化input值和样式
- if (range) {
- const { value = [0, 0] } = props;
- if (!Array.isArray(value)) {
- throw Error('Type of value must be array');
- }
- const realValue = value;
- changeInputValue(0, realValue[0]);
- changeInputValue(1, realValue[1]);
- } else {
- const { value = 0 } = props;
- if (Array.isArray(value)) {
- throw Error('Type of value must be string');
- }
- changeInputValue(0, value);
+ if (value <= max) {
+ return resetPoint * 0.5 + ((value - resetPoint) / (max - resetPoint)) * 50;
}
- }, []);
-
- // 组件卸载时删除stylesheet
- useEffect(
- () => () => {
- const styleSheet = document.styleSheets[0];
- if (!styleSheet) return;
- const beforeIndex = Array.from(styleSheet.rules).findIndex((item) => {
- if ('selectorText' in item) {
- return item.selectorText === `.${styles.slider}::before`;
+ }, [min, max, resetPoint, value]);
+
+ function handleMouseDown(e: React.MouseEvent<HTMLDivElement>) {
+ e.preventDefault();
+ const rail = sliderInnerRailRef.current!;
+ let isDragging = true;
+
+ function onMouseMove(e: MouseEvent) {
+ if (isDragging) {
+ const pureOffsetX = e.clientX - rail.getBoundingClientRect().x;
+
+ let offsetX = pureOffsetX;
+ if (offsetX <= 0) {
+ offsetX = 0;
+ } else if (offsetX >= styles.sliderWidth) {
+ offsetX = styles.sliderWidth;
}
- return false;
- });
- if (beforeIndex > -1) styleSheet.deleteRule(beforeIndex);
- const afterIndex = Array.from(styleSheet.rules).findIndex((item) => {
- if ('selectorText' in item) {
- return item.selectorText === `.${styles.slider}::after`;
+ const ratio = offsetX / styles.sliderWidth;
+
+ let value = 0;
+ if (ratio <= 0.5) {
+ value = (ratio * max) / 2;
+ } else {
+ value = resetPoint + (ratio - 0.5) * (max - resetPoint) * 2;
}
- return false;
- });
- if (afterIndex > -1) styleSheet.deleteRule(afterIndex);
- },
- []
- );
- const onInput = (e: React.FormEvent<HTMLInputElement>) => {
- const value = Number((e.target as HTMLInputElement).value);
- let index = 0;
- if (Array.isArray(props.value)) {
- if (value >= (valueNext - valuePrev) / 2 + valuePrev) {
- index = 1;
+ onChange && onChange(Math.ceil(value));
}
}
- props.onChange?.(e.nativeEvent);
-
- changeInputValue(index, value);
- };
-
- const onClick = (e: React.MouseEvent<HTMLDivElement>) => {
- const { min = 0, max = 100, step = 1 } = props;
- const target = e.currentTarget;
- const track = e.nativeEvent.offsetX;
- const width = target.offsetWidth;
- if (track < 0 || track > width) return;
- let value = (track / width) * (max - min);
- if (value % step) {
- value += step - (value % step);
- }
- let index = 0;
- if (Array.isArray(props.value)) {
- // Todo: 为何onInput触发的onClick值会有几px误差?
- if (value >= (valueNext - valuePrev) / 2 + valuePrev) {
- index = 1;
- }
+
+ function onMouseUp() {
+ isDragging = false;
+ document.removeEventListener('mousemove', onMouseMove);
+ document.removeEventListener('mouseup', onMouseUp);
}
- props.onClick?.(e.nativeEvent, value);
- changeInputValue(index, value);
- };
-
- const changeInputValue = (index: number, value: number) => {
- const parentNode = sliderRef.current;
- if (!parentNode) return;
- const input = parentNode.getElementsByTagName('input')[index];
- const id = input.id;
-
- parentNode.style.setProperty(`--${id}`, String(value));
-
- if (!index) {
- setValuePrev(value);
- } else {
- setValueNext(value);
+
+ function onMouseOut(e: MouseEvent) {
+ e.relatedTarget === null && onMouseUp();
}
- };
+
+ document.addEventListener('mousemove', onMouseMove);
+ document.addEventListener('mouseup', onMouseUp);
+ window.addEventListener('mouseout', onMouseOut);
+ }
+
+ console.log(shortcuts);
return (
- <div
- className={`${styles.slider} ${className}`}
- role="slider-group"
- aria-labelledby="multi-lbl"
- style={{ '--min': min, '--max': max } as CSSProperties}
- ref={sliderRef}
- onClick={(e) => onClick(e)}
- >
- <label className="sr-only" htmlFor={idPrevRef.current}></label>
- <input
- id={idPrevRef.current}
- type="range"
- min={min}
- value={valuePrev}
- max={max}
- step={step}
- onInput={(e) => onInput(e)}
- />
- {range && (
- <>
- <label className="sr-only" htmlFor={idNextRef.current}></label>
- <input
- id={idNextRef.current}
- type="range"
- min={min}
- value={valueNext}
- max={max}
- step={step}
- onInput={(e) => onInput(e)}
+ <div className={styles.slider}>
+ <Button type="text" disabled={value <= 0} onClick={() => handleStep(-10)}>
+ -
+ </Button>
+
+ <div className={styles.sliderRail}>
+ <div ref={sliderInnerRailRef} className={styles.sliderInnerRail}>
+ <Tooltip title={'恢复至 100%'}>
+ <a className={styles.sliderResetPoint} onClick={handleReset} />
+ </Tooltip>
+
+ <div
+ className={styles.sliderHandle}
+ role="slider"
+ style={{
+ left: `${offset}%`,
+ }}
+ onMouseDown={handleMouseDown}
/>
- </>
- )}
+ </div>
+ </div>
+
+ <Button type="text" disabled={value >= max} onClick={() => handleStep(10)}>
+ +
+ </Button>
+
+ <Dropwdown2
+ placement="topLeft"
+ overlay={
+ <div className={styles.sliderShortcuts}>
+ {shortcuts?.map((item) => (
+ <a
+ key={item}
+ className={joinClassNames(
+ styles.sliderShortcut,
+ item === value ? styles.sliderShortcutActive : ''
+ )}
+ onClick={() => onChange && onChange(item)}
+ >
+ {item === value && <span className={styles.sliderShortcutIcon}>✔</span>}
+ <span>{item}%</span>
+ </a>
+ ))}
+ </div>
+ }
+ >
+ <a className={styles.sliderValue}>{value}%</a>
+ </Dropwdown2>
</div>
);
}
diff --git a/packages/base-ui/src/Components/Slider/index.module.less b/packages/base-ui/src/Components/Slider/index.module.less
index c8df80b4dd..555a9918a8 100644
--- a/packages/base-ui/src/Components/Slider/index.module.less
+++ b/packages/base-ui/src/Components/Slider/index.module.less
@@ -1,99 +1,126 @@
@import '../../Basics/CSS/Skin/default.module.less';
-.slider {
- --dif: calc(var(--max) - var(--min));
- display: grid;
- grid-template: repeat(2, -webkit-max-content) 1em/1fr 1fr;
- grid-template: repeat(2, max-content) 1em/1fr 1fr;
- position: relative;
- margin: 1em auto;
- width: 100px;
- height: 2px;
- background: linear-gradient(0deg, #ccc 1em, transparent 0);
-}
+@slider-bg: @text-color-secondary;
+@slider-width: 144px;
+@slider-height: 4px;
+@slider-radius: 15px;
-.slider::before,
-.slider::after {
- grid-column: 1 / span 2;
- grid-row: 3;
- height: 2px;
- background: var(--primary-color);
- content: '';
-}
+@slider-handle-size: 20px;
-// .slider::before {
-// margin-left: calc((var(--a) - var(--min))/var(--dif)*100%);
-// width: calc((var(--b) - var(--a))/var(--dif)*100%);
-// }
+@slider-value-width: 76px;
-// .slider::after {
-// margin-left: calc((var(--b) - var(--min))/var(--dif)*100%);
-// width: calc((var(--a) - var(--b))/var(--dif)*100%);
-// }
+@slider-parts-hover-color: @border-color;
-[id='multi-lbl'] {
- grid-column: 1 / span 2;
+:export {
+ --slider-width: unit(@slider-width);
+ --slider-handle-size: unit(@slider-handle-size);
}
-.sr-only {
- position: absolute;
- -webkit-clip-path: inset(50%);
- clip-path: inset(50%);
-}
+.slider {
+ display: flex;
+ align-items: center;
+ gap: @margin-xs;
-input[type='range'] {
- grid-column: 1 / span 2;
- grid-row: 3;
- z-index: 1;
- top: 0;
- left: 0;
- margin: 0;
- background: none;
- /* get rid of white Chrome background */
- color: red;
- pointer-events: none;
-}
+ &-rail {
+ @padding: (@slider-handle-size / 2);
-input[type='range']::-webkit-slider-runnable-track,
-input[type='range']::-webkit-slider-thumb,
-input[type='range'] {
- -webkit-appearance: none;
-}
+ position: relative;
+ width: @slider-width;
+ height: @slider-height;
+ padding: 0 @padding;
+ background-color: @slider-bg;
+ border-radius: @slider-radius;
+ cursor: pointer;
+ }
-input[type='range']::-webkit-slider-runnable-track {
- width: 100%;
- height: 100%;
- background: none;
-}
+ &-inner-rail {
+ position: relative;
+ height: @slider-height;
+ }
-input[type='range']::-moz-range-track {
- width: 100%;
- height: 100%;
- background: none;
-}
+ &-handle {
+ position: absolute;
+ top: calc(50% - @slider-handle-size / 2);
+ width: @slider-handle-size;
+ height: @slider-handle-size;
+ background-color: @color-white;
+ border-radius: 50%;
+ box-shadow: @box-shadow-base;
+ transform: translateX(-50%);
+ cursor: pointer;
+ transition: background .2s;
+ &:hover {
+ background-color: @slider-parts-hover-color;
+ box-shadow: none;
+ }
+ }
-input[type='range']::-webkit-slider-thumb {
- border: 1px solid var(--primary-color);
- /* get rid of Firefox thumb border */
- width: 8px;
- height: 8px;
- border-radius: 50%;
- /* get rid of Firefox corner rounding */
- background: #fff;
- pointer-events: auto;
- transform: translateY(-50% + 8px);
- cursor: pointer;
-}
+ &-reset-point {
+ position: absolute;
+ width: @slider-height;
+ height: @slider-height;
+ background-color: @color-white;
+ border-radius: 50%;
+ left: calc(50% - @slider-height / 2);
+ cursor: pointer;
+ }
+
+ &-value {
+ width: @slider-value-width;
+ padding: @padding-xs 0;
+ text-align: center;
+ color: @text-color-secondary;
+ font-size: @font-size-lg;
+ font-weight: 700;
+ border-radius: @border-radius-base;
+ cursor: pointer;
+ transition: all .2s;
+ &:hover {
+ background-color: @slider-parts-hover-color;
+ }
+
+ &.dropdown2-open {
+ background-color: @slider-parts-hover-color;
+ }
+ }
+
+ &-shortcuts {
+ width: 128px;
+ padding: @padding-sm;
+ font-size: @font-size-base;
+ background-color: @color-white;
+ border: 1px solid @border-color;
+ border-radius: @border-radius-lg;
+ box-shadow: @box-shadow-lg;
+ box-sizing: border-box;
+ display: grid;
+ align-items: center;
+ gap: @margin-xs;
+ transform: translateX(-(@slider-value-width / 2));
+ }
+
+ &-shortcut {
+ position: relative;
+ padding: @padding-xs 0 @padding-xs 36px;
+ color: @text-color;
+ border-radius: @border-radius-base;
+ box-sizing: border-box;
+ cursor: pointer;
+ transition: background .2s;
+
+ &:hover {
+ background-color: @bg-color-hover;
+ }
+ }
+
+ &-shortcut-icon {
+ position: absolute;
+ left: @padding-xs;
+ top: 50%;
+ transform: translateY(-50%);
+ }
-input[type='range']::-moz-range-thumb {
- /* get rid of Firefox thumb border */
- border: 1px solid var(--primary-color);
- width: 8px;
- height: 8px;
- border-radius: 50%;
- /* get rid of Firefox corner rounding */
- background: #fff;
- pointer-events: auto;
- transform: translateY(-50% + 8px);
- cursor: pointer;
+ &-shortcut-active {
+ background-color: @bg-color-hover;
+ }
}
diff --git a/packages/base-ui/src/Components/Slider/test/index.test.tsx b/packages/base-ui/src/Components/Slider/test/index.test.tsx
index 87aff9d3a3..bd39006628 100644
--- a/packages/base-ui/src/Components/Slider/test/index.test.tsx
+++ b/packages/base-ui/src/Components/Slider/test/index.test.tsx
@@ -1,37 +1,11 @@
-import { fireEvent, render } from '@testing-library/react';
+import { render } from '@testing-library/react';
import { describe, expect, test } from 'vitest';
import { Slider } from '../Slider';
describe('Slider', () => {
test('renders correctly', () => {
- const { container } = render(<Slider min={0} max={50}></Slider>);
+ const { container } = render(<Slider min={0} max={100} value={90} />);
expect(container);
});
-
- test('click onClick function', () => {
- let a = 1;
-
- const { container } = render(
- <Slider
- id={'test'}
- onClick={() => {
- a++;
- }}
- />
- );
-
- fireEvent.click(container.querySelector('[role=slider-group]')!);
-
- expect(a).toEqual(2);
- });
-
- test('test change event', () => {
- let a = 1;
- const { container } = render(<Slider onChange={() => a++} />);
- const slider = container.querySelector('[role=slider-group]')!;
- const input = slider.querySelector('input');
- fireEvent.input(input!);
- expect(a).toEqual(2);
- });
});
diff --git a/packages/ui-plugin-sheets/src/View/CountBar/CountBar.tsx b/packages/ui-plugin-sheets/src/View/CountBar/CountBar.tsx
index 21d7bd216b..d4ba89a824 100644
--- a/packages/ui-plugin-sheets/src/View/CountBar/CountBar.tsx
+++ b/packages/ui-plugin-sheets/src/View/CountBar/CountBar.tsx
@@ -1,126 +1,30 @@
-import { AppContext, BaseComponentProps, Button, Icon, Slider } from '@univerjs/base-ui';
-import { Component, createRef } from 'react';
+import { Button, Icon, Slider } from '@univerjs/base-ui';
+import { useState } from 'react';
import styles from './index.module.less';
-export interface BaseCountBarProps extends BaseComponentProps {
- changeRatio: (ratio: string) => void;
-}
-
-interface CountState {
- zoom: number;
- content: string;
-}
-
-interface CountBarProps extends BaseComponentProps {
+interface ICountBarProps {
changeRatio?: (ratio: string) => void;
onChange?: (value: string) => void;
}
-export class CountBar extends Component<CountBarProps, CountState> {
- static override contextType = AppContext;
+const ZOOM_MAP = [50, 75, 100, 125, 150, 175, 200, 400];
- max = 100;
+export function CountBar(props: ICountBarProps) {
+ const [zoom, setZoom] = useState(100);
- min = 0;
+ function handleChange(value: number) {
+ setZoom(value);
- ref = createRef();
-
- constructor(props: CountBarProps) {
- super(props);
- this.initialize(props);
- }
-
- initialize(props: CountBarProps) {
- this.state = {
- zoom: 100,
- content: '',
- };
+ // TODO: changeRatio, onChange
}
- setValue = (value: object, fn?: () => void) => {
- this.setState(
- (prevState) => ({
- ...value,
- }),
- fn
- );
- };
-
- setZoom(zoom: number): void {
- if (zoom !== this.state.zoom) {
- this.setValue({
- zoom,
- });
- }
- }
-
- onChange = (e: Event) => {
- const target = e.target as HTMLInputElement;
- if (this.props.onChange) {
- this.props.onChange(target.value);
- }
- this.setValue({ zoom: target.value });
- };
-
- handleClick = (e: Event, value: number | string) => {
- this.setValue({ zoom: value });
- };
-
- addZoom = () => {
- const number = Math.floor(this.state.zoom / 10);
- let value = (number + 1) * 10;
- if (value >= this.max) value = this.max;
- this.setValue({ zoom: value });
- if (this.props.onChange) {
- this.props.onChange(String(value));
- }
- };
-
- reduceZoom = () => {
- const number = Math.ceil(this.state.zoom / 10);
- let value = (number - 1) * 10;
- if (value <= this.min) value = this.min;
- this.setValue({ zoom: value });
- if (this.props.onChange) {
- this.props.onChange(String(value));
- }
- };
-
- override render() {
- const { zoom, content } = this.state;
- // const PageIcon = this.getComponentRender().renderFunction('PageIcon');
- // const LayoutIcon = this.getComponentRender().renderFunction('LayoutIcon');
- return (
- <div className={styles.countBar}>
- <Button className={styles.countZoom}>{zoom}</Button>
- <Button onClick={this.addZoom}>
- <Icon.Math.AddIcon />
- </Button>
- <div className={styles.countSlider}>
- <Slider
- onChange={this.onChange}
- value={[20, 50]}
- min={this.min}
- max={this.max}
- range
- onClick={this.handleClick}
- />
- </div>
- <Button onClick={this.reduceZoom}>
- <Icon.Math.ReduceIcon />
- </Button>
- {/* <Button type="text">
- <PageIcon />
- </Button> */}
- {/* <Button type="text">
- <LayoutIcon />
- </Button> */}
- <Button>
- <Icon.Sheet.RegularIcon />
- </Button>
- <span className={styles.countStatistic}>{content}</span>
- </div>
- );
- }
+ return (
+ <section className={styles.countBar}>
+ <Button>
+ <Icon.Sheet.RegularIcon />
+ </Button>
+ <Slider value={zoom} shortcuts={ZOOM_MAP} onChange={handleChange} />
+ </section>
+ );
}
diff --git a/packages/ui-plugin-sheets/src/View/CountBar/index.module.less b/packages/ui-plugin-sheets/src/View/CountBar/index.module.less
index 27d3e523f3..8276aba007 100644
--- a/packages/ui-plugin-sheets/src/View/CountBar/index.module.less
+++ b/packages/ui-plugin-sheets/src/View/CountBar/index.module.less
@@ -1,32 +1,4 @@
.count-bar {
display: flex;
- flex-direction: row-reverse;
- // height: 22px;
- border-top: 1px solid var(--border-color-base);
- background-color: var(--bg);
- // line-height: 22px;
- font-size: 12px;
-
- .btn {
- height: auto;
- padding: 0;
- }
-
- svg {
- font-size: 21px;
- }
-
- .count-zoom {
- width: 35px;
- }
-
- .count-slider {
- width: 100px;
- }
-
- .count-statistic {
- margin-right: 1em;
- display: flex;
- align-items: center;
- }
+ justify-content: flex-end;
}