Skip to content

Commit

Permalink
fix toast z index bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jsladerman committed Dec 5, 2024
1 parent 72804be commit 95caf04
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/components/Layer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// this is deprecated and only still used internally for the Toast component (since it's probably more work than it's worth to migrate)
// anything else that needs similar functionality should use a ModalWrapper
import { type UseTransitionProps, useTransition } from '@react-spring/web'
import { useOutsideClick } from 'honorable'
import { isNil } from 'lodash-es'
import {
type ComponentProps,
type MutableRefObject,
type PropsWithChildren,
forwardRef,
Expand All @@ -9,8 +14,6 @@ import {
useState,
} from 'react'
import { createPortal } from 'react-dom'
import { type UseTransitionProps, useTransition } from '@react-spring/web'
import { isNil } from 'lodash-es'
import styled, { useTheme } from 'styled-components'

import usePrevious from '../hooks/usePrevious'
Expand Down Expand Up @@ -158,6 +161,7 @@ function LayerRef(
onClose,
onCloseComplete,
open,
wrapperProps,
}: PropsWithChildren<{
open: boolean
position: LayerPositionType
Expand All @@ -167,6 +171,7 @@ function LayerRef(
onClose?: () => void | null | undefined
onCloseComplete?: () => void | null | undefined
onClickOutside?: () => void | null | undefined
wrapperProps?: ComponentProps<'div'>
}>,
ref: MutableRefObject<HTMLDivElement>
) {
Expand Down Expand Up @@ -292,14 +297,15 @@ function LayerRef(
<LayerWrapper
$position={position}
$margin={margin}
{...wrapperProps}
>
{transitions((styles) => (
<AnimatedDiv
className="animated"
ref={finalRef}
style={{
...styles,
...(modal ? { zIndex: theme.zIndexes.modal } : {}),
...(modal ? { zIndex: theme.zIndexes.toast } : {}),
}}
>
{children}
Expand Down
13 changes: 12 additions & 1 deletion src/components/Toast.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { type Ref, forwardRef, useCallback, useEffect, useState } from 'react'
import {
type ComponentProps,
type Ref,
forwardRef,
useCallback,
useEffect,
useState,
} from 'react'

import { type Severity } from '../types'
import { type Extends } from '../utils/ts-utils'
Expand All @@ -15,6 +22,7 @@ type ToastProps = {
onCloseComplete?: () => void
show?: boolean
severity?: ToastSeverity
layerProps?: ComponentProps<'div'>
} & BannerProps

const defaults = {
Expand All @@ -35,6 +43,7 @@ const Toast = forwardRef(
severity = defaults.severity,
children,
show = true,
layerProps,
...props
}: ToastProps,
ref: Ref<any>
Expand Down Expand Up @@ -67,6 +76,7 @@ const Toast = forwardRef(

return (
<Layer
modal
open={open}
position={position}
onClose={() => {
Expand All @@ -76,6 +86,7 @@ const Toast = forwardRef(
onCloseComplete()
}}
ref={ref}
{...layerProps}
>
<Banner
onClose={() => setOpen(false)}
Expand Down
1 change: 1 addition & 0 deletions src/theme/zIndexes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export const zIndexes = {
modal: 1000,
selectPopover: 1500,
tooltip: 2000,
toast: 2500,
} as const satisfies CSSObject

0 comments on commit 95caf04

Please sign in to comment.