Skip to content

Commit

Permalink
Move height ref to <Toaster/>
Browse files Browse the repository at this point in the history
  • Loading branch information
timolins committed Mar 14, 2021
1 parent 802fe9e commit 06e271c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
16 changes: 1 addition & 15 deletions src/components/toast-bar.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand All @@ -41,8 +39,6 @@ const Message = styled('div')`

interface ToastBarProps {
toast: Toast;
onHeight: (height: number) => void;

position: ToastPosition;
}

Expand All @@ -67,16 +63,7 @@ const getAnimationStyle = (
};

export const ToastBar: React.FC<ToastBarProps> = 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 };
Expand All @@ -96,7 +83,6 @@ export const ToastBar: React.FC<ToastBarProps> = React.memo(

return (
<ToastBarBase
ref={ref}
className={toast.className}
style={{
...animationStyle,
Expand Down
15 changes: 10 additions & 5 deletions src/components/toaster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import CSS from 'csstype';
import { useToaster } from '../core/use-toaster';
import { ToastBar } from './toast-bar';
import { ToastPosition, DefaultToastOptions } from '../core/types';
import { createRectRef } from '../core/utils';

setup(React.createElement);

Expand Down Expand Up @@ -70,19 +71,23 @@ export const Toaster: React.FC<ToasterProps> = ({
});
const positionStyle = getPositionStyle(position, offset);

const ref = t.height
? undefined
: createRectRef((rect) => {
handlers.updateHeight(t.id, rect.height);
});

return (
<div
ref={ref}
key={t.id}
style={{
zIndex: t.visible ? 9999 : undefined,
margin: 16,
...positionStyle,
}}
>
<ToastBar
onHeight={(height) => handlers.updateHeight(t.id, height)}
toast={t}
position={position}
/>
<ToastBar toast={t} position={position} />
</div>
);
})}
Expand Down
11 changes: 11 additions & 0 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
};

0 comments on commit 06e271c

Please sign in to comment.