From 06e271c594129423411379814b0c53e8d949f2e2 Mon Sep 17 00:00:00 2001 From: Timo Lins Date: Sun, 14 Mar 2021 21:37:12 +0100 Subject: [PATCH] Move height ref to --- src/components/toast-bar.tsx | 16 +--------------- src/components/toaster.tsx | 15 ++++++++++----- src/core/utils.ts | 11 +++++++++++ 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/components/toast-bar.tsx b/src/components/toast-bar.tsx index 5cc9bba..68af70b 100644 --- a/src/components/toast-bar.tsx +++ b/src/components/toast-bar.tsx @@ -1,5 +1,4 @@ import * as React from 'react'; -import { useCallback } from 'react'; import { styled, keyframes } from 'goober'; import { Toast, ToastPosition, resolveValueOrFunction } from '../core/types'; @@ -25,7 +24,6 @@ const ToastBarBase = styled('div', React.forwardRef)` will-change: transform; box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1), 0 3px 3px rgba(0, 0, 0, 0.05); max-width: 350px; - margin: 16px; pointer-events: auto; padding: 8px 10px; border-radius: 8px; @@ -41,8 +39,6 @@ const Message = styled('div')` interface ToastBarProps { toast: Toast; - onHeight: (height: number) => void; - position: ToastPosition; } @@ -67,16 +63,7 @@ const getAnimationStyle = ( }; export const ToastBar: React.FC = React.memo( - ({ toast, position, onHeight }) => { - const ref = useCallback((el: HTMLElement | null) => { - if (el) { - setTimeout(() => { - const boundingRect = el.getBoundingClientRect(); - onHeight(boundingRect.height); - }); - } - }, []); - + ({ toast, position }) => { const animationStyle = toast?.height ? getAnimationStyle(position, toast.visible) : { opacity: 0 }; @@ -96,7 +83,6 @@ export const ToastBar: React.FC = React.memo( return ( = ({ }); const positionStyle = getPositionStyle(position, offset); + const ref = t.height + ? undefined + : createRectRef((rect) => { + handlers.updateHeight(t.id, rect.height); + }); + return (
- handlers.updateHeight(t.id, height)} - toast={t} - position={position} - /> +
); })} diff --git a/src/core/utils.ts b/src/core/utils.ts index fdaca37..5393032 100644 --- a/src/core/utils.ts +++ b/src/core/utils.ts @@ -4,3 +4,14 @@ export const genId = (() => { return (++count).toString(); }; })(); + +export const createRectRef = (onRect: (rect: DOMRect) => void) => ( + el: HTMLElement | null +) => { + if (el) { + setTimeout(() => { + const boundingRect = el.getBoundingClientRect(); + onRect(boundingRect); + }); + } +};