Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/components/toast-bar.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as React from 'react';
import { styled, keyframes } from 'goober';

import { Toast, ToastPosition, resolveValue, Renderable } from '../core/types';
import { Toast, ToastPosition, Renderable } from '../core/types';
import { ToastIcon } from './toast-icon';
import { prefersReducedMotion } from '../core/utils';
import { prefersReducedMotion, resolveValue } from '../core/utils';

const enterAnimation = (factor: number) => `
0% {transform: translate3d(0,${factor * -200}%,0) scale(.6); opacity:.5;}
Expand Down Expand Up @@ -72,9 +72,9 @@ export const ToastBar: React.FC<ToastBarProps> = React.memo(
({ toast, position, style, children }) => {
const animationStyle: React.CSSProperties = toast?.height
? getAnimationStyle(
toast.position || position || 'top-center',
toast.visible
)
toast.position || position || 'top-center',
toast.visible
)
: { opacity: 0 };

const icon = <ToastIcon toast={toast} />;
Expand Down
15 changes: 7 additions & 8 deletions src/components/toaster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import {
ToastPosition,
DefaultToastOptions,
Toast,
resolveValue,
} from '../core/types';
import { createRectRef, prefersReducedMotion } from '../core/utils';
import { createRectRef, prefersReducedMotion, resolveValue } from '../core/utils';

setup(React.createElement);

Expand All @@ -21,13 +20,13 @@ const getPositionStyle = (
const verticalStyle: React.CSSProperties = top ? { top: 0 } : { bottom: 0 };
const horizontalStyle: React.CSSProperties = position.includes('center')
? {
justifyContent: 'center',
}
justifyContent: 'center',
}
: position.includes('right')
? {
? {
justifyContent: 'flex-end',
}
: {};
: {};
return {
left: 0,
right: 0,
Expand Down Expand Up @@ -100,8 +99,8 @@ export const Toaster: React.FC<ToasterProps> = ({
const ref = t.height
? undefined
: createRectRef((rect) => {
handlers.updateHeight(t.id, rect.height);
});
handlers.updateHeight(t.id, rect.height);
});

return (
<div
Expand Down
3 changes: 1 addition & 2 deletions src/core/toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ import {
ToastType,
DefaultToastOptions,
ValueOrFunction,
resolveValue,
} from './types';
import { genId } from './utils';
import { genId, resolveValue } from './utils';
import { dispatch, ActionType } from './store';

type Message = ValueOrFunction<Renderable, Toast>;
Expand Down
10 changes: 0 additions & 10 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@ export type ValueOrFunction<TValue, TArg> =
| TValue
| ValueFunction<TValue, TArg>;

const isFunction = <TValue, TArg>(
valOrFunction: ValueOrFunction<TValue, TArg>
): valOrFunction is ValueFunction<TValue, TArg> =>
typeof valOrFunction === 'function';

export const resolveValue = <TValue, TArg>(
valOrFunction: ValueOrFunction<TValue, TArg>,
arg: TArg
): TValue => (isFunction(valOrFunction) ? valOrFunction(arg) : valOrFunction);

export interface Toast {
type: ToastType;
id: string;
Expand Down
12 changes: 12 additions & 0 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ValueFunction, ValueOrFunction } from "./types";

export const genId = (() => {
let count = 0;
return () => {
Expand Down Expand Up @@ -28,3 +30,13 @@ export const prefersReducedMotion = (() => {
return shouldReduceMotion;
};
})();

const isFunction = <TValue, TArg>(
valOrFunction: ValueOrFunction<TValue, TArg>
): valOrFunction is ValueFunction<TValue, TArg> =>
typeof valOrFunction === 'function';

export const resolveValue = <TValue, TArg>(
valOrFunction: ValueOrFunction<TValue, TArg>,
arg: TArg
): TValue => (isFunction(valOrFunction) ? valOrFunction(arg) : valOrFunction);
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export { ToastBar } from './components/toast-bar';
export { ToastIcon } from './components/toast-icon';
export { Toaster } from './components/toaster';
export { useStore as useToasterStore } from './core/store';
export { resolveValue } from './core/types';
export { resolveValue } from './core/utils';

export type ToastOptions = _ToastOptions;
export type ToastPosition = _ToastPosition;
Expand Down