-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Slider): redesign API to get more control on marks (#1744)
- Loading branch information
Showing
12 changed files
with
431 additions
and
168 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
88 changes: 88 additions & 0 deletions
88
src/components/Slider/HandleWithTooltip/HandleWithTooltip.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,88 @@ | ||
import React from 'react'; | ||
|
||
import {SliderTooltip} from '../SliderTooltip/SliderTooltip'; | ||
import type {HandleWithTooltipProps} from '../types'; | ||
|
||
export const HandleWithTooltip = ({ | ||
originHandle, | ||
originHandleProps, | ||
stateModifiers, | ||
tooltipFormat, | ||
className, | ||
}: HandleWithTooltipProps) => { | ||
const autoVisible = stateModifiers['tooltip-display'] === 'auto'; | ||
const alwaysVisible = stateModifiers['tooltip-display'] === 'on'; | ||
const [tooltipVisible, setTooltipVisible] = React.useState(false); | ||
const [focused, setFocused] = React.useState(false); | ||
const [hovered, setHovered] = React.useState(false); | ||
|
||
if (alwaysVisible && !tooltipVisible) { | ||
setTooltipVisible(true); | ||
} | ||
|
||
//to show tooltip on mobile devices on touch | ||
if (autoVisible && !tooltipVisible && originHandleProps.dragging) { | ||
setTooltipVisible(true); | ||
} | ||
|
||
const styleProp = stateModifiers.rtl ? 'right' : 'left'; | ||
const tooltipContent = tooltipFormat | ||
? tooltipFormat(originHandleProps.value) | ||
: originHandleProps.value; | ||
|
||
const handleTooltipVisibility = ({ | ||
currentHovered, | ||
currentFocused, | ||
}: { | ||
currentHovered?: boolean; | ||
currentFocused?: boolean; | ||
}) => { | ||
if (autoVisible) { | ||
const handleHovered = currentHovered === undefined ? hovered : currentHovered; | ||
const handleFocused = currentFocused === undefined ? focused : currentFocused; | ||
setTooltipVisible(handleHovered || handleFocused); | ||
} | ||
}; | ||
|
||
const handle = alwaysVisible | ||
? originHandle | ||
: React.cloneElement(originHandle, { | ||
onMouseEnter: (event: React.MouseEvent<HTMLDivElement>) => { | ||
originHandle.props.onMouseEnter?.(event); | ||
setHovered(true); | ||
handleTooltipVisibility({currentHovered: true}); | ||
}, | ||
onMouseLeave: (event: React.MouseEvent<HTMLDivElement>) => { | ||
originHandle.props.onMouseLeave?.(event); | ||
setHovered(false); | ||
handleTooltipVisibility({currentHovered: false}); | ||
}, | ||
onFocus: (event: React.FocusEvent<HTMLDivElement>) => { | ||
originHandle.props.onFocus?.(event); | ||
setFocused(true); | ||
handleTooltipVisibility({currentFocused: true}); | ||
}, | ||
onBlur: (event: React.FocusEvent<HTMLDivElement>) => { | ||
originHandle.props.onBlur?.(event); | ||
setFocused(false); | ||
handleTooltipVisibility({currentFocused: false}); | ||
}, | ||
}); | ||
|
||
return ( | ||
<React.Fragment> | ||
{handle} | ||
{tooltipVisible && ( | ||
<SliderTooltip | ||
className={className} | ||
style={{ | ||
insetInlineStart: originHandle.props.style?.[styleProp], | ||
}} | ||
stateModifiers={stateModifiers} | ||
> | ||
{tooltipContent} | ||
</SliderTooltip> | ||
)} | ||
</React.Fragment> | ||
); | ||
}; |
Oops, something went wrong.