Skip to content

Commit

Permalink
Fix initial position of Slider
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasSlama committed Sep 20, 2024
1 parent 1de0649 commit 5f1400c
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 14 deletions.
8 changes: 8 additions & 0 deletions .changeset/thick-peaches-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@toptal/picasso-slider': patch
'@toptal/picasso': patch
---

### Slider

- fix initial position of Tooltip
5 changes: 5 additions & 0 deletions .changeset/tiny-news-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@toptal/picasso-carousel': patch
---

- refactor useOnScreen usage after the breaking change
13 changes: 13 additions & 0 deletions .changeset/twelve-years-play.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'@toptal/picasso-utils': major
'@toptal/picasso': major
---

### useOnScreen

- change return value of the hook to let component know when the oberver starts observing

```diff
-const isOnScreen = useOnScreen({...})
+const { isOnScreen, isObserved } = useOnScreen({...})
```
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const useAutoplay = ({
rewind,
isLastPage,
}: Props) => {
const isOnScreen = useOnScreen({ ref: wrapperRef })
const { isOnScreen } = useOnScreen({ ref: wrapperRef })
const isMouseOver = useMouseEnter(wrapperRef)

useInterval({
Expand Down
17 changes: 12 additions & 5 deletions packages/base/Slider/src/Slider/Slider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// import type { ComponentProps } from 'react'
import React, { forwardRef, useRef } from 'react'
import React, { forwardRef, useEffect, useRef } from 'react'
import { Slider as MUIBaseSlider } from '@mui/base/Slider'
import { useCombinedRefs, useOnScreen } from '@toptal/picasso-utils'
import { twJoin, twMerge } from '@toptal/picasso-tailwind-merge'
Expand Down Expand Up @@ -66,7 +66,7 @@ export const Slider = forwardRef<HTMLElement, Props>(function Slider(
marks,
value,
defaultValue = 0,
tooltip = 'off',
tooltip: externalTooltip = 'off',
tooltipFormat,
step,
disabled,
Expand All @@ -82,6 +82,7 @@ export const Slider = forwardRef<HTMLElement, Props>(function Slider(
'data-private': dataPrivate,
'data-testid': dataTestid,
} = props
const [tooltip, setTooltip] = React.useState('off')
const containerRef = useRef<HTMLDivElement>(null)
const sliderRef = useCombinedRefs<HTMLElement>(ref, useRef<HTMLElement>(null))
const { isPartiallyOverlapped, handleValueLabelOnRender } = useLabelOverlap({
Expand All @@ -93,12 +94,18 @@ export const Slider = forwardRef<HTMLElement, Props>(function Slider(

// The rootMargin is not working correctly in the storybooks iframe
// To test properly we can open the iframe in new window
const isContainerOnScreen = useOnScreen({
const { isOnScreen, isObserved } = useOnScreen({
ref: containerRef,
rootMargin: '-24px 0px 0px 0px',
threshold: 1,
})

useEffect(() => {
if (containerRef.current) {
setTooltip(externalTooltip)
}
}, [externalTooltip])

return (
<div
ref={containerRef}
Expand Down Expand Up @@ -153,9 +160,9 @@ export const Slider = forwardRef<HTMLElement, Props>(function Slider(
),
},
valueLabel: {
tooltip,
tooltip: isObserved ? tooltip : 'off',
onRender: handleValueLabelOnRender,
yPlacement: isContainerOnScreen ? 'top' : 'bottom',
yPlacement: isOnScreen ? 'top' : 'bottom',
isOverlaped: isPartiallyOverlapped,
},
}}
Expand Down
10 changes: 6 additions & 4 deletions packages/base/Slider/src/Slider/story/Default.example.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import React from 'react'
import React, { useState } from 'react'
import { Container, Slider } from '@toptal/picasso'

type Value = number | number[]

const Example = () => {
const handleChange = (_: React.ChangeEvent<{}>, value: Value) => {
window.console.log('onChange: ', value)
const [value, setValue] = useState<Value>(0)

const handleChange = (_: Event, newValue: Value) => {
setValue(newValue)
}

return (
<Container>
<Slider onChange={handleChange} />
<Slider value={value} onChange={handleChange} />
</Container>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const SliderValueLabel = ({
})
)
// we need to recalculate on value change to get new rect
}, [isOverlaped, index, xPlacement, value])
}, [isOverlaped, index, xPlacement, value, tooltip])

return (
<span
Expand Down
6 changes: 4 additions & 2 deletions packages/base/Utils/src/utils/useOnScreen/use-on-screen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ const useOnScreen = ({
rootMargin,
threshold,
}: UseOnScreenProps) => {
const [isIntersecting, setIntersecting] = useState(false)
const [isOnScreen, setIntersecting] = useState(false)
const [isObserved, setObserved] = useState(false)

const observer = useMemo(
() =>
new IntersectionObserver(
([entry]) => {
setIntersecting(entry.isIntersecting)
setObserved(true)
},
{
root: root?.current,
Expand All @@ -44,7 +46,7 @@ const useOnScreen = ({
}
}, [observer, ref])

return isIntersecting
return { isOnScreen, isObserved }
}

export default useOnScreen
5 changes: 4 additions & 1 deletion tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

/** @type {import('tailwindcss').Config} */
module.exports = {
content: ['packages/**/*.{ts,tsx}'],
content: [
'packages/base/*/src/**/*.{ts,tsx}',
'packages/*/src/**/*.{ts,tsx}',
],
presets: [
require('@toptal/base-tailwind'),
require('@toptal/picasso-tailwind'),
Expand Down

0 comments on commit 5f1400c

Please sign in to comment.