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

fix(react-slider): Export internal CSS vars #33682

Open
wants to merge 8 commits 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
Copy link
Collaborator

@fabricteam fabricteam Jan 17, 2025

Choose a reason for hiding this comment

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

🕵🏾‍♀️ visual regressions to review in the fluentuiv9 Visual Regression Report

Avatar Converged 2 screenshots
Image Name Diff(in Pixels) Image Type
Avatar Converged.badgeMask.chromium.png 5 Changed
Avatar Converged.Badge Mask RTL.chromium.png 4 Changed
Drawer 1 screenshots
Image Name Diff(in Pixels) Image Type
Drawer.Full Overlay RTL.chromium.png 1167 Changed

"type": "patch",
"comment": "fix: exported internal CSS variables",
"packageName": "@fluentui/react-slider",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@ export const sliderClassNames: SlotClassNames<SliderSlots>;
// @public (undocumented)
export const sliderCSSVars: {
sliderDirectionVar: string;
sliderInnerThumbRadiusVar: string;
sliderProgressVar: string;
sliderProgressColorVar: string;
sliderRailSizeVar: string;
sliderRailColorVar: string;
sliderStepsPercentVar: string;
sliderThumbColorVar: string;
sliderThumbSizeVar: string;
};

// @public (undocumented)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,31 @@ export const sliderClassNames: SlotClassNames<SliderSlots> = {
};

// Internal CSS variables
const thumbSizeVar = `--fui-Slider__thumb--size`;
const innerThumbRadiusVar = `--fui-Slider__inner-thumb--radius`;
const thumbPositionVar = `--fui-Slider__thumb--position`;
const railSizeVar = `--fui-Slider__rail--size`;
const railColorVar = `--fui-Slider__rail--color`;
const progressColorVar = `--fui-Slider__progress--color`;
const thumbColorVar = `--fui-Slider__thumb--color`;

export const sliderCSSVars = {
sliderDirectionVar: `--fui-Slider--direction`,
sliderInnerThumbRadiusVar: `--fui-Slider__inner-thumb--radius`,
sliderProgressVar: `--fui-Slider--progress`,
sliderProgressColorVar: `--fui-Slider__progress--color`,
sliderRailSizeVar: `--fui-Slider__rail--size`,
sliderRailColorVar: `--fui-Slider__rail--color`,
sliderStepsPercentVar: `--fui-Slider--steps-percent`,
sliderThumbColorVar: `--fui-Slider__thumb--color`,
sliderThumbSizeVar: `--fui-Slider__thumb--size`,
};

const { sliderDirectionVar, sliderStepsPercentVar, sliderProgressVar } = sliderCSSVars;
Copy link
Member

Choose a reason for hiding this comment

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

deconstructing all the vars out here saves a bunch of code below. 61 vars with sliderCSSVars prefix. sliderCSSVars.sliderThumbSizeVar could simply be sliderThumbSizeVar

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added it this way because of this comment:
#32939 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

If there is good reason to remove the destructuring, that's fine.

const {
sliderDirectionVar,
sliderInnerThumbRadiusVar,
sliderProgressVar,
sliderProgressColorVar,
sliderRailSizeVar,
sliderRailColorVar,
sliderStepsPercentVar,
sliderThumbColorVar,
sliderThumbSizeVar,
} = sliderCSSVars;

/**
* Styles for the root slot
Expand All @@ -41,64 +51,64 @@ const useRootStyles = makeStyles({
},

small: {
[thumbSizeVar]: '16px',
[innerThumbRadiusVar]: '5px',
[railSizeVar]: '2px',
[sliderThumbSizeVar]: '16px',
[sliderInnerThumbRadiusVar]: '5px',
[sliderRailSizeVar]: '2px',
minHeight: '24px',
},

medium: {
[thumbSizeVar]: '20px',
[innerThumbRadiusVar]: '6px',
[railSizeVar]: '4px',
[sliderThumbSizeVar]: '20px',
[sliderInnerThumbRadiusVar]: '6px',
[sliderRailSizeVar]: '4px',
minHeight: '32px',
},

horizontal: {
minWidth: '120px',
// 3x3 grid with the rail and thumb in the center cell [2,2] and the hidden input stretching across all cells
gridTemplateRows: `1fr var(${thumbSizeVar}) 1fr`,
gridTemplateColumns: `1fr calc(100% - var(${thumbSizeVar})) 1fr`,
gridTemplateRows: `1fr var(${sliderThumbSizeVar}) 1fr`,
gridTemplateColumns: `1fr calc(100% - var(${sliderThumbSizeVar})) 1fr`,
},

vertical: {
minHeight: '120px',
// 3x3 grid with the rail and thumb in the center cell [2,2] and the hidden input stretching across all cells
gridTemplateRows: `1fr calc(100% - var(${thumbSizeVar})) 1fr`,
gridTemplateColumns: `1fr var(${thumbSizeVar}) 1fr`,
gridTemplateRows: `1fr calc(100% - var(${sliderThumbSizeVar})) 1fr`,
gridTemplateColumns: `1fr var(${sliderThumbSizeVar}) 1fr`,
},

enabled: {
[railColorVar]: tokens.colorNeutralStrokeAccessible,
[progressColorVar]: tokens.colorCompoundBrandBackground,
[thumbColorVar]: tokens.colorCompoundBrandBackground,
[sliderRailColorVar]: tokens.colorNeutralStrokeAccessible,
[sliderProgressColorVar]: tokens.colorCompoundBrandBackground,
[sliderThumbColorVar]: tokens.colorCompoundBrandBackground,
':hover': {
[thumbColorVar]: tokens.colorCompoundBrandBackgroundHover,
[progressColorVar]: tokens.colorCompoundBrandBackgroundHover,
[sliderThumbColorVar]: tokens.colorCompoundBrandBackgroundHover,
[sliderProgressColorVar]: tokens.colorCompoundBrandBackgroundHover,
},
':active': {
[thumbColorVar]: tokens.colorCompoundBrandBackgroundPressed,
[progressColorVar]: tokens.colorCompoundBrandBackgroundPressed,
[sliderThumbColorVar]: tokens.colorCompoundBrandBackgroundPressed,
[sliderProgressColorVar]: tokens.colorCompoundBrandBackgroundPressed,
},
'@media (forced-colors: active)': {
[railColorVar]: 'CanvasText',
[thumbColorVar]: 'Highlight',
[progressColorVar]: 'Highlight',
[sliderRailColorVar]: 'CanvasText',
[sliderThumbColorVar]: 'Highlight',
[sliderProgressColorVar]: 'Highlight',
':hover': {
[thumbColorVar]: 'Highlight',
[progressColorVar]: 'Highlight',
[sliderThumbColorVar]: 'Highlight',
[sliderProgressColorVar]: 'Highlight',
},
},
},

disabled: {
[thumbColorVar]: tokens.colorNeutralForegroundDisabled,
[railColorVar]: tokens.colorNeutralBackgroundDisabled,
[progressColorVar]: tokens.colorNeutralForegroundDisabled,
[sliderThumbColorVar]: tokens.colorNeutralForegroundDisabled,
[sliderRailColorVar]: tokens.colorNeutralBackgroundDisabled,
[sliderProgressColorVar]: tokens.colorNeutralForegroundDisabled,
'@media (forced-colors: active)': {
[railColorVar]: 'GrayText',
[thumbColorVar]: 'GrayText',
[progressColorVar]: 'GrayText',
[sliderRailColorVar]: 'GrayText',
[sliderCSSVars.sliderThumbColorVar]: 'GrayText',
[sliderCSSVars.sliderProgressColorVar]: 'GrayText',
},
},

Expand Down Expand Up @@ -129,9 +139,9 @@ const useRailStyles = makeStyles({
// Background gradient represents the progress of the slider
backgroundImage: `linear-gradient(
var(${sliderDirectionVar}),
var(${progressColorVar}) 0%,
var(${progressColorVar}) var(${sliderProgressVar}),
var(${railColorVar}) var(${sliderProgressVar})
var(${sliderProgressColorVar}) 0%,
var(${sliderProgressColorVar}) var(${sliderProgressVar}),
var(${sliderRailColorVar}) var(${sliderProgressVar})
)`,
outlineWidth: '1px',
outlineStyle: 'solid',
Expand Down Expand Up @@ -162,19 +172,19 @@ const useRailStyles = makeStyles({

horizontal: {
width: '100%',
height: `var(${railSizeVar})`,
height: `var(${sliderRailSizeVar})`,
'::before': {
left: '-1px',
right: '-1px',
height: `var(${railSizeVar})`,
height: `var(${sliderRailSizeVar})`,
},
},

vertical: {
width: `var(${railSizeVar})`,
width: `var(${sliderRailSizeVar})`,
height: '100%',
'::before': {
width: `var(${railSizeVar})`,
width: `var(${sliderRailSizeVar})`,
top: '-1px',
bottom: '-1px',
},
Expand All @@ -189,20 +199,20 @@ const useThumbStyles = makeStyles({
// Ensure the thumb stays within the track boundaries.
// When the value is at 0% or 100%, the distance from the track edge
// to the thumb center equals the inner thumb radius.
[`${thumbPositionVar}`]: `clamp(var(${innerThumbRadiusVar}), var(${sliderProgressVar}), calc(100% - var(${innerThumbRadiusVar})))`,
[`${thumbPositionVar}`]: `clamp(var(${sliderInnerThumbRadiusVar}), var(${sliderProgressVar}), calc(100% - var(${sliderInnerThumbRadiusVar})))`,
gridRowStart: '2',
gridRowEnd: '2',
gridColumnStart: '2',
gridColumnEnd: '2',
position: 'absolute',
width: `var(${thumbSizeVar})`,
height: `var(${thumbSizeVar})`,
width: `var(${sliderThumbSizeVar})`,
height: `var(${sliderThumbSizeVar})`,
pointerEvents: 'none',
outlineStyle: 'none',
forcedColorAdjust: 'none',
borderRadius: tokens.borderRadiusCircular,
boxShadow: `0 0 0 calc(var(${thumbSizeVar}) * .2) ${tokens.colorNeutralBackground1} inset`,
backgroundColor: `var(${thumbColorVar})`,
boxShadow: `0 0 0 calc(var(${sliderThumbSizeVar}) * .2) ${tokens.colorNeutralBackground1} inset`,
backgroundColor: `var(${sliderThumbColorVar})`,
'::before': {
position: 'absolute',
top: '0px',
Expand All @@ -212,12 +222,12 @@ const useThumbStyles = makeStyles({
borderRadius: tokens.borderRadiusCircular,
boxSizing: 'border-box',
content: "''",
border: `calc(var(${thumbSizeVar}) * .05) solid ${tokens.colorNeutralStroke1}`,
border: `calc(var(${sliderThumbSizeVar}) * .05) solid ${tokens.colorNeutralStroke1}`,
},
},
disabled: {
'::before': {
border: `calc(var(${thumbSizeVar}) * .05) solid ${tokens.colorNeutralForegroundDisabled}`,
border: `calc(var(${sliderThumbSizeVar}) * .05) solid ${tokens.colorNeutralForegroundDisabled}`,
},
},
horizontal: {
Expand Down Expand Up @@ -248,12 +258,12 @@ const useInputStyles = makeStyles({
cursor: 'default',
},
horizontal: {
height: `var(${thumbSizeVar})`,
height: `var(${sliderThumbSizeVar})`,
width: '100%',
},
vertical: {
height: '100%',
width: `var(${thumbSizeVar})`,
width: `var(${sliderThumbSizeVar})`,
'-webkit-appearance': 'slider-vertical',
},
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as React from 'react';
import { useId, Label, Slider, makeStyles, sliderCSSVars, tokens } from '@fluentui/react-components';

const { sliderProgressColorVar, sliderRailColorVar, sliderThumbColorVar, sliderThumbSizeVar } = sliderCSSVars;

const useStyles = makeStyles({
enabled: {
[sliderProgressColorVar]: '#ff0764',
[sliderRailColorVar]: tokens.colorNeutralForeground1,
[sliderThumbColorVar]: '#ff0764',
':hover': {
[sliderThumbColorVar]: '#d60d58',
[sliderProgressColorVar]: '#d60d58',
':active': {
[sliderThumbColorVar]: '#b91150',
[sliderProgressColorVar]: '#b91150',
},
},
},
thumb: {
[sliderThumbSizeVar]: '16px',
boxShadow: `0 0 0 calc(var(${sliderThumbSizeVar}) * .2) ${tokens.colorTransparentBackground} inset`,
'::before': {
content: 'unset',
},
},
});

export const Customized = () => {
const id = useId();
const styles = useStyles();

return (
<>
<Label htmlFor={id}>Customized slider</Label>
<Slider className={styles.enabled} thumb={{ className: styles.thumb }} defaultValue={20} size="small" id={id} />
</>
);
};

Customized.parameters = {
docs: {
description: {
story: 'Customized slider using its variables',
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export { Step } from './SliderStep.stories';
export { MinMax } from './SliderMinMax.stories';
export { Vertical } from './SliderVertical.stories';
export { Disabled } from './SliderDisabled.stories';
export { Customized } from './SliderCustomized.stories';

export default {
title: 'Components/Slider',
Expand Down
Loading