From 40ea35e6fd5cc11f384bc03777f583f094f9734c Mon Sep 17 00:00:00 2001 From: Kolja Lampe Date: Thu, 9 Nov 2023 17:46:45 +0100 Subject: [PATCH] Allow easier styling of mark and dot Similar to the other components we have like `tracks`, `rail` and `handle`, this allows easier styling via classes or inline styles. I've also separated the active styles of both, as being able to style, how a selected range should look is very helpful. --- src/Marks/Mark.tsx | 20 ++++++++++++++------ src/Steps/Dot.tsx | 19 +++++++++++++------ src/interface.ts | 16 ++++++++++++++-- 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/src/Marks/Mark.tsx b/src/Marks/Mark.tsx index bcbb362d6..fbb6ce3e6 100644 --- a/src/Marks/Mark.tsx +++ b/src/Marks/Mark.tsx @@ -1,7 +1,7 @@ +import cls from 'classnames'; import * as React from 'react'; -import classNames from 'classnames'; -import { getDirectionStyle } from '../util'; import SliderContext from '../context'; +import { getDirectionStyle } from '../util'; export interface MarkProps { prefixCls: string; @@ -13,22 +13,30 @@ export interface MarkProps { export default function Mark(props: MarkProps) { const { prefixCls, style, children, value, onClick } = props; - const { min, max, direction, includedStart, includedEnd, included } = + const { min, max, direction, includedStart, includedEnd, included, styles, classNames } = React.useContext(SliderContext); const textCls = `${prefixCls}-text`; + const active = included && includedStart <= value && value <= includedEnd; // ============================ Offset ============================ const positionStyle = getDirectionStyle(direction, value, min, max); return ( { e.stopPropagation(); diff --git a/src/Steps/Dot.tsx b/src/Steps/Dot.tsx index a8eaa4bef..25c00277d 100644 --- a/src/Steps/Dot.tsx +++ b/src/Steps/Dot.tsx @@ -1,7 +1,7 @@ +import cls from 'classnames'; import * as React from 'react'; -import classNames from 'classnames'; -import { getDirectionStyle } from '../util'; import SliderContext from '../context'; +import { getDirectionStyle } from '../util'; export interface DotProps { prefixCls: string; @@ -12,7 +12,7 @@ export interface DotProps { export default function Dot(props: DotProps) { const { prefixCls, value, style, activeStyle } = props; - const { min, max, direction, included, includedStart, includedEnd } = + const { min, max, direction, included, includedStart, includedEnd, styles, classNames } = React.useContext(SliderContext); const dotClassName = `${prefixCls}-dot`; @@ -22,20 +22,27 @@ export default function Dot(props: DotProps) { let mergedStyle = { ...getDirectionStyle(direction, value, min, max), ...(typeof style === 'function' ? style(value) : style), + ...styles.dot, }; if (active) { mergedStyle = { ...mergedStyle, ...(typeof activeStyle === 'function' ? activeStyle(value) : activeStyle), + ...styles.dotActive, }; } return ( ); diff --git a/src/interface.ts b/src/interface.ts index 82f0b59ea..988801483 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -2,11 +2,23 @@ import type React from 'react'; export type Direction = 'rtl' | 'ltr' | 'ttb' | 'btt'; -export type OnStartMove = (e: React.MouseEvent | React.TouchEvent, valueIndex: number, startValues?: number[]) => void; +export type OnStartMove = ( + e: React.MouseEvent | React.TouchEvent, + valueIndex: number, + startValues?: number[], +) => void; export type AriaValueFormat = (value: number) => string; -export type SemanticName = 'tracks' | 'track' | 'rail' | 'handle'; +export type SemanticName = + | 'tracks' + | 'track' + | 'rail' + | 'handle' + | 'dot' + | 'dotActive' + | 'mark' + | 'markActive'; export type SliderClassNames = Partial>;