-
Notifications
You must be signed in to change notification settings - Fork 10
feat(range): add custom ticks & correct rect size #934
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
5 changes: 3 additions & 2 deletions
5
packages/ods-react/src/components/range/src/components/range-bounds/RangeBounds.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
packages/ods-react/src/components/range/src/components/range-bounds/rangeBounds.module.scss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
packages/ods-react/src/components/range/src/components/range-tick/RangeTick.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { Slider } from '@ark-ui/react/slider'; | ||
import classNames from 'classnames'; | ||
import { type FC, type JSX, useEffect, useRef } from 'react'; | ||
import { THUMB_SIZE } from '../../constants/thumb'; | ||
import { type RangeTickItem, useRange } from '../../contexts/useRange'; | ||
import style from './rangeTick.module.scss'; | ||
|
||
interface RangeTickProp { | ||
index: number, | ||
isLast: boolean, | ||
singleMode: boolean, | ||
tick: RangeTickItem, | ||
} | ||
|
||
const RangeTick: FC<RangeTickProp> = ({ | ||
index, | ||
isLast, | ||
singleMode, | ||
tick, | ||
}): JSX.Element => { | ||
const { disabled, setRootPadding } = useRange(); | ||
const tickRef = useRef<HTMLSpanElement>(null); | ||
const isNumber = typeof tick === 'number'; | ||
|
||
useEffect(() => { | ||
if (!tickRef.current || typeof tick === 'number') { | ||
return; | ||
} | ||
|
||
const resizeObserver = new ResizeObserver((entries) => { | ||
if (entries && entries.length) { | ||
const { height, top, width } = entries[0].contentRect; | ||
|
||
if (index === 0) { | ||
setRootPadding((padding) => ({ | ||
...padding, | ||
left: Math.max(0, (width / 2) - (THUMB_SIZE / 2)), | ||
})); | ||
} else if (isLast) { | ||
setRootPadding((padding) => ({ | ||
...padding, | ||
right: Math.max(0, (width / 2) - (THUMB_SIZE / 2)), | ||
})); | ||
} | ||
|
||
setRootPadding((padding) => ({ | ||
...padding, | ||
bottom: height + top, | ||
})); | ||
} | ||
}); | ||
|
||
resizeObserver.observe(tickRef.current); | ||
|
||
return () => { | ||
resizeObserver.disconnect(); | ||
}; | ||
}, [index, isLast, setRootPadding, tick, tickRef]); | ||
|
||
return ( | ||
<Slider.Marker | ||
className={ classNames( | ||
style['range-tick'], | ||
{ [style['range-tick--custom-marker']]: !isNumber }, | ||
{ [style['range-tick--single-mode']]: singleMode }, | ||
)} | ||
ref={ tickRef } | ||
value={ isNumber ? tick : tick.value }> | ||
{ | ||
!isNumber && | ||
<span className={ classNames( | ||
style['range-tick__label'], | ||
{ [style['range-tick__label--disabled']]: disabled }, | ||
)}> | ||
{ tick.label } | ||
</span> | ||
} | ||
</Slider.Marker> | ||
); | ||
}; | ||
|
||
RangeTick.displayName = 'RangeTick'; | ||
|
||
export { | ||
RangeTick, | ||
type RangeTickProp, | ||
}; |
47 changes: 47 additions & 0 deletions
47
packages/ods-react/src/components/range/src/components/range-tick/rangeTick.module.scss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
@use 'sass:math'; | ||
@use '../../../../../style/range'; | ||
|
||
@layer ods-atoms { | ||
.range-tick { | ||
$tick-width: 2px; | ||
|
||
position: absolute; | ||
|
||
&::before { | ||
display: block; | ||
position: absolute; | ||
bottom: -(math.div(range.$ods-range-thumb-size, 2) - math.div(range.$ods-range-track-height, 2)); | ||
border-radius: 6px; | ||
background-color: range.$ods-range-background-color; | ||
width: $tick-width; | ||
height: range.$ods-range-thumb-size; | ||
content: ''; | ||
} | ||
|
||
&[data-state="at-value"], | ||
&--single-mode[data-state="under-value"] { | ||
&::before { | ||
background-color: range.$ods-range-background-color-active; | ||
} | ||
} | ||
|
||
&--custom-marker { | ||
padding-top: range.$ods-range-label-padding-top; | ||
|
||
&::before { | ||
top: -(range.$ods-range-track-height + math.div(range.$ods-range-thumb-size - range.$ods-range-track-height, 2)); | ||
bottom: auto; | ||
left: calc(50% - ($tick-width / 2)); | ||
} | ||
} | ||
|
||
&__label { | ||
color: var(--ods-color-text); | ||
font-weight: 600; | ||
|
||
&--disabled { | ||
color: var(--ods-color-text-disabled-default); | ||
} | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
packages/ods-react/src/components/range/src/components/range-ticks/RangeTicks.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Slider } from '@ark-ui/react/slider'; | ||
import { type FC, type JSX } from 'react'; | ||
import { type RangeTickItem } from '../../contexts/useRange'; | ||
import { RangeTick } from '../range-tick/RangeTick'; | ||
import style from './rangeTicks.module.scss'; | ||
|
||
interface RangeTicksProp { | ||
singleMode: boolean, | ||
ticks: RangeTickItem[], | ||
} | ||
|
||
const RangeTicks: FC<RangeTicksProp> = ({ | ||
singleMode, | ||
ticks, | ||
}): JSX.Element => { | ||
return ( | ||
<Slider.MarkerGroup className={ style['range-ticks'] }> | ||
{ | ||
ticks.map((tick, i) => ( | ||
<RangeTick | ||
index={ i } | ||
isLast={ i === ticks.length - 1 } | ||
key={ typeof tick === 'number' ? tick : tick.value } | ||
singleMode={ singleMode } | ||
tick={ tick } /> | ||
)) | ||
} | ||
</Slider.MarkerGroup> | ||
); | ||
}; | ||
|
||
RangeTicks.displayName = 'RangeTicks'; | ||
|
||
export { | ||
RangeTicks, | ||
type RangeTicksProp, | ||
}; |
5 changes: 5 additions & 0 deletions
5
packages/ods-react/src/components/range/src/components/range-ticks/rangeTicks.module.scss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@layer ods-atoms { | ||
.range-ticks { | ||
position: relative; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.