-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
[Slider] Use percentage values in slider styles #2341
Conversation
Restore PEMDASPreview: documentation | landing | table |
@reiv dude yes this is brilliant! do we even need |
@giladgray Had to leave that in because there was some stuff in the slider handle code ( blueprint/packages/core/src/components/slider/handle.tsx Lines 93 to 94 in 66bf867
Perhaps that can be rewritten too, but I don't fully understand it yet. |
Unrelated to this issue, but it looks like there's a possible oversight in blueprint/packages/core/src/components/slider/handle.tsx Lines 56 to 58 in 6de4f5a
I'm assuming Was there ever a plan to permit controlling the handle size? Anyway, Edit: Hmm, looks like I misunderstood, and it was there for a very different reason: the range slider handle is special in that its midpoint is actually on the visible edge of the handle. But |
Restore previous logic with added commentPreview: documentation | landing | table |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@reiv this looks so great!! i'll look into removing tickSize
after merging this. just one thought about numeric precision.
const offset = Math.round((value - min) * tickSize - handleMidpoint); | ||
const style: React.CSSProperties = vertical ? { bottom: offset } : { left: offset }; | ||
const offsetRatio = (value - min) * tickSizeRatio; | ||
const offsetCalc = `calc(${offsetRatio * 100}% - ${handleMidpoint}px)`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👌
const initialValue = clamp(this.props.initialValue, this.props.min, this.props.max); | ||
|
||
let offset = Math.round((initialValue - this.props.min) * tickSize); | ||
let size = Math.round((this.props.value - initialValue) * tickSize); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's cool that we don't need to round
anymore, but i think we want to ensure a maximum number of decimal places via number.toFixed(2)
to reduce sub-pixel rendering.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean rounding the value of offsetRatio
? I don't think that would accomplish much because the actual computed value in pixels doesn't have any restrictions on the number of decimal places. Here's an example of a round percentage yielding sub-pixel precision (shows inline styles and computed value from inspector):
Unfortunately CSS calc
provides no facilities for rounding the result. 😢
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no i mean ${(offset * 100).toFixed(2)}%
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sub-pixel renders are going to happen here for sure, but there's a limit to precision on the screen. it's definitely possible that my suggestion isn't really helpful?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, at the very least we'll get inline styles that are easy on the eyes if anyone happens to be snooping around with inspect element. I'll make it happen 👍
@giladgray I believe |
RenamesPreview: documentation | landing | table |
Fix testPreview: documentation | landing | table |
const offsetPercentage = `${offset * 100}%`; | ||
const sizePercentage = `${size * 100}%`; | ||
const offsetPercentage = `${(offset * 100).toFixed(2)}%`; | ||
const sizePercentage = `${(size * 100).toFixed(2)}%`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok well now we should definitely have a formatPercentage(size)
utility function!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it could go in coreSlider.tsx
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok sure.
@@ -193,7 +193,7 @@ describe("<Slider>", () => { | |||
const style = renderSlider(<Slider initialValue={-10} min={0} value={5} />) | |||
.find("." + Classes.SLIDER_PROGRESS) | |||
.prop("style") as React.CSSProperties; | |||
assert.strictEqual(style.left, "0%"); | |||
assert.strictEqual(style.left, "0.00%"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah... always exactly 2 decimals.
Add formatPercentage helper functionPreview: documentation | landing | table |
RenamesPreview: documentation | landing | table |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hooray!
Fixes #2338
Changes proposed in this pull request:
This PR changes the slider styles to use percentages instead of absolute pixel values. This should allow the slider to be freely resized without requiring a forced re-render.
Screenshot
This recording shows the slider being resized (
width
is being set in the inspector).