Skip to content

Commit

Permalink
feat: Add TooltipContext to set global timeout values
Browse files Browse the repository at this point in the history
  • Loading branch information
frankieyan committed Oct 26, 2024
1 parent c78bf42 commit 9221737
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Reactist follows [semantic versioning](https://semver.org/) and doesn't introduce breaking changes (API-wise) in minor or patch releases. However, the appearance of a component might change in a minor or patch release so keep an eye on redesigns and make sure your app still looks and feels like you expect it.

# Next

- [Feat] Add `TooltipContext` to allow `showTimeout` and `hideTimeout` to be set globally

# v26.1.0

- [Feat] Expose `showTimeout` and `hideTimeout` props for `Tooltip`
Expand Down
49 changes: 45 additions & 4 deletions src/tooltip/tooltip.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react'
import { createPortal } from 'react-dom'

import { Tooltip, TooltipProps } from './tooltip'
import { Tooltip, TooltipContext, TooltipProps } from './tooltip'
import { Button } from '../button'
import { Stack } from '../stack'
import { TextField } from '../text-field'
Expand Down Expand Up @@ -138,9 +138,10 @@ TooltipRichContent.argTypes = {
},
}

// This story sets a new z-index for the tooltip, but it will leak into other stories
// when viewed from the Docs page, since all stories live in a single iframe.
// Only in the Canvas page will stories be isolated from each other
//
// Custom Z Index
//

export function TooltipCustomZIndex() {
return (
<>
Expand Down Expand Up @@ -176,3 +177,43 @@ export function TooltipCustomZIndex() {
function PortalToHead({ children }: React.PropsWithChildren<unknown>) {
return createPortal(children, document.head)
}

//
// Tooltip Global Context
//

export function TooltipGlobalContext({
showTimeout,
hideTimeout,
}: Required<Pick<TooltipProps, 'showTimeout' | 'hideTimeout'>>) {
const contextValue = React.useMemo(() => ({ showTimeout, hideTimeout }), [
showTimeout,
hideTimeout,
])

return (
<Stack space="medium">
<Text>
<code>{'<TooltipContext>'}</code> can be used to provide global settings to all
tooltips:
</Text>

<TooltipContext.Provider value={contextValue}>
<Box padding="large" display="flex" gap="medium">
<Tooltip content="Click here to begin your journey">
<Button variant="primary">Got it</Button>
</Tooltip>

<Tooltip content="Click here to return">
<Button variant="secondary">Cancel</Button>
</Tooltip>
</Box>
</TooltipContext.Provider>
</Stack>
)
}

TooltipGlobalContext.args = {
showTimeout: 1000,
hideTimeout: 2000,
}
27 changes: 23 additions & 4 deletions src/tooltip/tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ import type { TooltipStoreState } from '@ariakit/react'
import styles from './tooltip.module.css'
import type { ObfuscatedClassName } from '../utils/common-types'

type TooltipContextState = {
showTimeout: number
hideTimeout: number
}

const TooltipContext = React.createContext<TooltipContextState>({
showTimeout: 500,
hideTimeout: 100,
})

interface TooltipProps extends ObfuscatedClassName {
/**
* The element that triggers the tooltip. Generally a button or link.
Expand Down Expand Up @@ -69,12 +79,14 @@ interface TooltipProps extends ObfuscatedClassName {

/**
* The amount of time in milliseconds to wait before showing the tooltip
* Use `<TooltipContext.Provider>` to set a global value for all tooltips
* @default 500
*/
showTimeout?: number

/**
* The amount of time in milliseconds to wait before hiding the tooltip
* Use `<TooltipContext.Provider>` to set a global value for all tooltips
* @default 100
*/
hideTimeout?: number
Expand All @@ -86,11 +98,18 @@ function Tooltip({
position = 'top',
gapSize = 3,
withArrow = false,
showTimeout = 500,
hideTimeout = 100,
showTimeout,
hideTimeout,
exceptionallySetClassName,
}: TooltipProps) {
const tooltip = useTooltipStore({ placement: position, showTimeout, hideTimeout })
const { showTimeout: globalShowTimeout, hideTimeout: globalHideTimeout } = React.useContext(
TooltipContext,
)
const tooltip = useTooltipStore({
placement: position,
showTimeout: showTimeout ?? globalShowTimeout,
hideTimeout: hideTimeout ?? globalHideTimeout,
})
const isOpen = tooltip.useState('open')

const child = React.Children.only(
Expand Down Expand Up @@ -135,4 +154,4 @@ function Tooltip({
}

export type { TooltipProps }
export { Tooltip }
export { Tooltip, TooltipContext }

0 comments on commit 9221737

Please sign in to comment.