-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
LemonSlider
and use it in flag rollout conditions (#19958)
* Add `LemonSlider` * Replace Ant `Slider` with `LemonSlider` * Use `LemonSlider` in feature flags * Fix slider sizing * Update UI snapshots for `chromium` (1) * Fix touch and add ring * Reduce transition duration slightly * Restore utilities.scss * Update UI snapshots for `chromium` (1) * Remove leftover comment --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
a9ea061
commit 35a3c45
Showing
12 changed files
with
191 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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 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
17 changes: 17 additions & 0 deletions
17
frontend/src/lib/lemon-ui/LemonSlider/LemonSlider.stories.tsx
This file contains 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,17 @@ | ||
import { Meta } from '@storybook/react' | ||
import { useState } from 'react' | ||
|
||
import { LemonSlider } from './LemonSlider' | ||
|
||
const meta: Meta<typeof LemonSlider> = { | ||
title: 'Lemon UI/Lemon Slider', | ||
component: LemonSlider, | ||
tags: ['autodocs'], | ||
} | ||
export default meta | ||
|
||
export function Basic(): JSX.Element { | ||
const [value, setValue] = useState(42) | ||
|
||
return <LemonSlider value={value} min={0} max={100} step={1} onChange={setValue} /> | ||
} |
This file contains 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,104 @@ | ||
import clsx from 'clsx' | ||
import { useEventListener } from 'lib/hooks/useEventListener' | ||
import { useRef, useState } from 'react' | ||
|
||
export interface LemonSliderProps { | ||
value?: number | ||
onChange?: (value: number) => void | ||
min: number | ||
max: number | ||
/** @default 1 */ | ||
step?: number | ||
className?: string | ||
} | ||
|
||
export function LemonSlider({ value = 0, onChange, min, max, step = 1, className }: LemonSliderProps): JSX.Element { | ||
const trackRef = useRef<HTMLDivElement>(null) | ||
const movementStartValueWithX = useRef<[number, number] | null>(null) | ||
const [dragging, setDragging] = useState(false) | ||
|
||
const handleMove = (clientX: number): void => { | ||
if (trackRef.current && movementStartValueWithX.current !== null) { | ||
const [movementStartValue, movementStartX] = movementStartValueWithX.current | ||
const rect = trackRef.current.getBoundingClientRect() | ||
const adjustedWidth = rect.width - 16 // 16px = handle width | ||
const deltaX = (clientX - movementStartX) / adjustedWidth | ||
let newValue = movementStartValue + (max - min) * deltaX | ||
newValue = Math.max(min, Math.min(max, newValue)) // Clamped | ||
if (step !== undefined) { | ||
newValue = Math.round(newValue / step) * step // Adjusted to step | ||
} | ||
onChange?.(newValue) | ||
} | ||
} | ||
useEventListener('mousemove', (e) => { | ||
handleMove(e.clientX) | ||
}) | ||
useEventListener('touchmove', (e) => { | ||
if (e.touches.length === 1) { | ||
handleMove(e.touches[0].clientX) | ||
} | ||
}) | ||
|
||
useEventListener('mouseup', (e) => { | ||
if (e.button === 0) { | ||
movementStartValueWithX.current = null | ||
setDragging(false) | ||
} | ||
}) | ||
useEventListener('touchend', () => { | ||
movementStartValueWithX.current = null | ||
setDragging(false) | ||
}) | ||
useEventListener('touchcancel', () => { | ||
movementStartValueWithX.current = null | ||
setDragging(false) | ||
}) | ||
|
||
const proportion = Math.round(((value - min) / (max - min)) * 100) / 100 | ||
|
||
return ( | ||
<div className={clsx('flex items-center relative my-2.5 min-w-16 select-none', className)}> | ||
<div | ||
className="w-full h-3 flex items-center cursor-pointer" | ||
ref={trackRef} | ||
onMouseDown={(e) => { | ||
const rect = e.currentTarget.getBoundingClientRect() | ||
const x = e.clientX - (rect.left + 8) // 4px = half the handle | ||
const adjustedWidth = rect.width - 16 // 8px = handle width | ||
let newValue = (x / adjustedWidth) * (max - min) + min | ||
newValue = Math.max(min, Math.min(max, newValue)) // Clamped | ||
if (step !== undefined) { | ||
newValue = Math.round(newValue / step) * step // Adjusted to step | ||
} | ||
onChange?.(newValue) | ||
}} | ||
> | ||
<div className="w-full bg-border rounded-full h-1" /> | ||
</div> | ||
<div | ||
className="absolute h-1 bg-primary rounded-full pointer-events-none" | ||
// eslint-disable-next-line react/forbid-dom-props | ||
style={{ width: `${proportion * 100}%` }} | ||
/> | ||
<div | ||
className={clsx( | ||
'absolute size-3 box-content border-2 border-bg-light rounded-full cursor-pointer bg-primary transition-shadow duration-75', | ||
dragging ? 'ring-2 scale-90' : 'ring-0 hover:ring-2' | ||
)} | ||
// eslint-disable-next-line react/forbid-dom-props | ||
style={{ | ||
left: `calc(${proportion * 100}% - ${proportion}rem)`, | ||
}} | ||
onMouseDown={(e) => { | ||
movementStartValueWithX.current = [value, e.clientX] | ||
setDragging(true) | ||
}} | ||
onTouchStart={(e) => { | ||
movementStartValueWithX.current = [value, e.touches[0].clientX] | ||
setDragging(true) | ||
}} | ||
/> | ||
</div> | ||
) | ||
} |
This file contains 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 @@ | ||
export { LemonSlider, type LemonSliderProps } from './LemonSlider' |
This file contains 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 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 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 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