Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow easier styling of mark and dot #949

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/Marks/Mark.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 (
<span
className={classNames(textCls, {
[`${textCls}-active`]: included && includedStart <= value && value <= includedEnd,
})}
className={cls(
textCls,
{
[`${textCls}-active`]: active,
},
active && classNames.markActive,
classNames.mark,
)}
style={{
...positionStyle,
...style,
...(active ? styles.markActive : {}),
...styles.mark,
Comment on lines +38 to +39
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be:

...styles.mark,
...(active ? styles.markActive : {}),

markActive styles should override the default mark styles, not the other way around.

}}
onMouseDown={(e) => {
e.stopPropagation();
Expand Down
19 changes: 13 additions & 6 deletions src/Steps/Dot.tsx
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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`;
Expand All @@ -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 (
<span
className={classNames(dotClassName, {
[`${dotClassName}-active`]: active,
})}
className={cls(
dotClassName,
{
[`${dotClassName}-active`]: active,
},
active && classNames.dotActive,
classNames.dot,
)}
style={mergedStyle}
/>
);
Expand Down
16 changes: 14 additions & 2 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment on lines +5 to +9
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary changes.


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<Record<SemanticName, string>>;

Expand Down
Loading