-
Notifications
You must be signed in to change notification settings - Fork 21
feat(component): add raw slider #6435
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
Open
alizedebray
wants to merge
1
commit into
slider-component-utils
Choose a base branch
from
5854-web-component-func-slider
base: slider-component-utils
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
102 changes: 102 additions & 0 deletions
102
packages/components/src/components/post-slider/active-thumb.ts
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,102 @@ | ||
| import { Orientation } from '@root/src'; | ||
|
|
||
| interface Bounds { | ||
| min: number; | ||
| max: number; | ||
| } | ||
|
|
||
| interface Neighbors { | ||
| previous: HTMLElement | null; | ||
| next: HTMLElement | null; | ||
| } | ||
|
|
||
| function isThumb(node: Node | EventTarget): node is HTMLElement { | ||
| return node instanceof HTMLElement && node.matches('[role="slider"]'); | ||
| } | ||
|
|
||
| function getValueNow(thumb: HTMLElement): number { | ||
| return parseFloat(thumb.getAttribute('aria-valuenow')); | ||
| } | ||
|
|
||
| export class ActiveThumb { | ||
| el: HTMLElement; | ||
| neighbors: Neighbors; | ||
| positionBounds: Bounds; | ||
| hostBounds: Bounds; | ||
|
|
||
| private host: HTMLElement; | ||
| private relativePosition = 0; | ||
| private isPositionUpdating = false; | ||
|
|
||
| get isOnlyThumb(): boolean { | ||
| return !this.neighbors.previous && !this.neighbors.next; | ||
| } | ||
|
|
||
| get isFirstThumb(): boolean { | ||
| return !this.isOnlyThumb && !this.neighbors.previous; | ||
| } | ||
|
|
||
| get value(): number { | ||
| return getValueNow(this.el); | ||
| } | ||
|
|
||
| get neighborValues(): { previous: number | null; next: number | null } { | ||
| const previousValue = this.neighbors.previous ? getValueNow(this.neighbors.previous) : null; | ||
| const nextValue = this.neighbors.next ? getValueNow(this.neighbors.next) : null; | ||
| return { previous: previousValue, next: nextValue }; | ||
| } | ||
|
|
||
| constructor(node: Node | EventTarget, host: HTMLElement, orientation: Orientation) { | ||
| if (!isThumb(node)) throw Error('An active thumb must be an HTML element with a slider role.'); | ||
|
|
||
| this.el = node; | ||
| this.neighbors = { | ||
| previous: isThumb(this.el.previousSibling) ? this.el.previousSibling : null, | ||
| next: isThumb(this.el.nextSibling) ? this.el.nextSibling : null, | ||
| }; | ||
|
|
||
| this.host = host; | ||
| this.hostBounds = this.getHostBounds(host, orientation); | ||
|
|
||
| const minBound = this.neighbors.previous | ||
| ? this.getOffset(this.neighbors.previous, orientation) | ||
| : this.hostBounds.min; | ||
| const maxBound = this.neighbors.next | ||
| ? this.getOffset(this.neighbors.next, orientation) | ||
| : this.hostBounds.max; | ||
| this.positionBounds = { min: minBound, max: maxBound }; | ||
|
|
||
| this.updatePosition = this.updatePosition.bind(this); | ||
| } | ||
|
|
||
| private getHostBounds(host: HTMLElement, orientation: Orientation): Bounds { | ||
| const rect = host.getBoundingClientRect(); | ||
| return orientation === 'vertical' | ||
| ? { min: rect.top, max: rect.bottom } | ||
| : { min: rect.left, max: rect.right }; | ||
| } | ||
|
|
||
| private getOffset(el: HTMLElement, orientation: Orientation): number { | ||
| const rect = el.getBoundingClientRect(); | ||
| return orientation === 'vertical' ? rect.top + rect.height / 2 : rect.left + rect.width / 2; | ||
| } | ||
|
|
||
| private updatePosition() { | ||
| this.isPositionUpdating = true; | ||
|
|
||
| const cssProperty = this.isFirstThumb ? '--post-slider-fill-start' : '--post-slider-fill-end'; | ||
| this.host.style.setProperty(cssProperty, this.relativePosition.toString()); | ||
|
|
||
| requestAnimationFrame(this.updatePosition); | ||
| } | ||
|
|
||
| setValue(value: number, relativePosition: number) { | ||
| this.el.setAttribute('aria-valuenow', value.toString()); | ||
| this.neighbors.previous?.setAttribute('aria-valuemax', value.toString()); | ||
| this.neighbors.next?.setAttribute('aria-valuemin', value.toString()); | ||
|
|
||
| // updating position depending on the animation frame rate | ||
| this.relativePosition = relativePosition; | ||
| if (!this.isPositionUpdating) this.updatePosition(); | ||
| } | ||
| } | ||
3 changes: 3 additions & 0 deletions
3
packages/components/src/components/post-slider/orientation.ts
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,3 @@ | ||
| export const ORIENTATIONS = ['horizontal', 'vertical'] as const; | ||
|
|
||
| export type Orientation = (typeof ORIENTATIONS)[number]; |
85 changes: 85 additions & 0 deletions
85
packages/components/src/components/post-slider/post-slider.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,85 @@ | ||
| @use '@swisspost/design-system-styles/variables/elevation'; | ||
|
|
||
| :host { | ||
| --post-slider-thumb-size: 1.75rem; | ||
| --post-slider-track-size: .75rem; | ||
| --post-slider-fill-start: 0; | ||
| --post-slider-fill-end: 1; | ||
|
|
||
| display: block; | ||
| position: relative; | ||
| height: var(--post-slider-thumb-size); | ||
| container-type: inline-size; | ||
| touch-action: none; | ||
|
|
||
| &::before, | ||
| &::after { | ||
| content: ''; | ||
| display: block; | ||
| position: absolute; | ||
| inset-block: calc(50% - var(--post-slider-track-size) / 2); | ||
| inset-inline: 0; | ||
| } | ||
|
|
||
| // slider track | ||
| &::before { | ||
| background-color: #f0efed; | ||
| } | ||
|
|
||
| // slider fill | ||
| &::after { | ||
| --post-slider-fill-scale: calc(var(--post-slider-fill-end) - var(--post-slider-fill-start)); | ||
| background-color: #050400; | ||
| transform-origin: left; | ||
| transform: translateX(calc(var(--post-slider-fill-start) * 100cqw)) scaleX(var(--post-slider-fill-scale)); | ||
| } | ||
|
|
||
| // slider thumbs | ||
| [role="slider"] { | ||
| --post-slider-thumb-scale: 1; | ||
|
|
||
| z-index: 1; | ||
| box-sizing: border-box; | ||
| height: var(--post-slider-thumb-size); | ||
| width: var(--post-slider-thumb-size); | ||
| margin-inline-start: calc(var(--post-slider-thumb-size) / -2); | ||
| position: absolute; | ||
| inset-block-start: 0; | ||
| inset-inline-start: 0; | ||
| background-color: #050400; | ||
| border: 3px solid #fff; | ||
| border-radius: 50%; | ||
| box-shadow: elevation.$elevation-100; | ||
| will-change: transform; | ||
| transform: translateX(calc(var(--post-slider-thumb-position) * 100cqw)) scale(var(--post-slider-thumb-scale)); | ||
| transition: background-color 200ms; | ||
|
|
||
| &:not(:last-child) { | ||
| --post-slider-thumb-position: var(--post-slider-fill-start); | ||
| } | ||
|
|
||
| &:last-child { | ||
| --post-slider-thumb-position: var(--post-slider-fill-end); | ||
| } | ||
|
|
||
| &:is(:hover, .active) { | ||
| background-color: #504f4b; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| :host([orient="vertical"]) { | ||
| writing-mode: vertical-lr; | ||
| height: unset; | ||
| min-height: 15rem; | ||
| width: var(--post-slider-thumb-size); | ||
|
|
||
| &::after { | ||
| transform-origin: top; | ||
| transform: translateY(calc(var(--post-slider-fill-start) * 100cqh)) scaleY(var(--post-slider-fill-scale)); | ||
| } | ||
|
|
||
| [role="slider"] { | ||
| transform: translateY(calc(var(--post-slider-thumb-position) * 100cqh)) scale(var(--post-slider-thumb-scale)); | ||
| } | ||
| } |
Oops, something went wrong.
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.
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.
The constructor is only called
onPointerDownof the div with therole="slider", thereforeisThumb(node)should always be true so I'm not sure it's worth checking here.