Skip to content

Commit

Permalink
feat(@vtmn/react, @vtmn/vue): timeout enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
Tlahey committed Feb 15, 2024
1 parent a87f7ea commit 0fb0d05
Show file tree
Hide file tree
Showing 16 changed files with 152 additions and 27 deletions.
9 changes: 9 additions & 0 deletions packages/showcases/core/csf/components/overlays/alert.csf.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,13 @@ export const argTypes = {
defaultValue: undefined,
control: { type: 'text' },
},

timeout: {
type: { name: 'number', required: false },
description: 'Duration of the animation',
defaultValue: 8000,
control: {
type: 'number',
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ export const argTypes = {
type: 'text',
},
},
timeout: {
type: { name: 'number', required: false },
description: 'Duration of the animation',
defaultValue: 4500,
control: {
type: 'number',
},
},
};
8 changes: 8 additions & 0 deletions packages/showcases/core/csf/components/overlays/toast.csf.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ export const argTypes = {
type: 'boolean',
},
},
timeout: {
type: { name: 'number', required: false },
description: 'Duration of the animation',
defaultValue: 4500,
control: {
type: 'number',
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
import { VtmnAlert } from '@vtmn/react';
import { VtmnButton } from '@vtmn/react';

const CSS_ANIMATION_TIME_MS = 700;

export default {
title: 'Components / Overlays / VtmnAlert',
component: VtmnAlert,
Expand All @@ -21,7 +23,7 @@ const OverviewTemplate: Story = (args) => {
if (showAlert) {
const timeout = setTimeout(() => {
setShowAlert(false);
}, 8000);
}, args.timeout + CSS_ANIMATION_TIME_MS);
return () => clearTimeout(timeout);
}
}, [showAlert]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
import { VtmnSnackbar } from '@vtmn/react';
import { VtmnButton } from '@vtmn/react';

const CSS_ANIMATION_TIME_MS = 500;

export default {
title: 'Components / Overlays / VtmnSnackbar',
component: VtmnSnackbar,
Expand All @@ -21,7 +23,7 @@ const OverviewTemplate: Story = (args) => {
if (showSnackbar) {
const timeout = setTimeout(() => {
setshowSnackbar(false);
}, 8000);
}, args.timeout + CSS_ANIMATION_TIME_MS);
return () => clearTimeout(timeout);
}
}, [showSnackbar]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
import { VtmnToast } from '@vtmn/react';
import { VtmnButton } from '@vtmn/react';

const CSS_ANIMATION_TIME_MS = 500;

export default {
title: 'Components / Overlays / VtmnToast',
component: VtmnToast,
Expand All @@ -21,7 +23,7 @@ const OverviewTemplate: Story = (args) => {
if (showToast) {
const timeout = setTimeout(() => {
setshowToast(false);
}, 8000);
}, args.timeout + CSS_ANIMATION_TIME_MS);
return () => clearTimeout(timeout);
}
}, [showToast]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,7 @@
<Meta
title="Components / Overlays / VtmnSnackbar"
component={VtmnSnackbar}
argTypes={{
...argTypes,
timeout: {
type: { name: 'number', required: false },
description: 'Duration of the animation',
defaultValue: 4500,
control: {
type: 'number',
},
},
}}
{argTypes}
parameters={{
...parameters,
readme: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
parameters,
} from '@vtmn/showcase-core/csf/components/overlays/alert.csf';

const CSS_ANIMATION_TIME_MS = 700;

export default {
title: 'Components / Overlays / VtmnAlert',
component: VtmnAlert,
Expand All @@ -23,7 +25,7 @@ const Template = (args) => ({
showAlert.value = true;
setTimeout(() => {
showAlert.value = false;
}, 8000);
}, args.timeout + CSS_ANIMATION_TIME_MS);
},
args,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { ref } from 'vue';
import { VtmnSnackbar, VtmnButton } from '@vtmn/vue';
import { parameters } from '@vtmn/showcase-core/csf/components/overlays/snackbar.csf';

const CSS_ANIMATION_TIME_MS = 500;

export default {
title: 'Components / Overlays / VtmnSnackbar',
component: VtmnSnackbar,
Expand All @@ -22,6 +24,14 @@ export default {
type: 'text',
},
},
timeout: {
type: { name: 'number', required: false },
description: 'Duration of the animation',
defaultValue: 4500,
control: {
type: 'number',
},
},
},
parameters,
};
Expand All @@ -37,7 +47,7 @@ const Template = (args) => ({
showSnackbar.value = true;
setTimeout(() => {
showSnackbar.value = false;
}, 8000);
}, args.timeout + CSS_ANIMATION_TIME_MS);
},
args,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { ref } from 'vue';
import { VtmnToast, VtmnButton } from '@vtmn/vue';
import { parameters } from '@vtmn/showcase-core/csf/components/overlays/toast.csf';

const CSS_ANIMATION_TIME_MS = 500;

export default {
title: 'Components / Overlays / VtmnToast',
component: VtmnToast,
Expand All @@ -22,6 +24,14 @@ export default {
type: 'boolean',
},
},
timeout: {
type: { name: 'number', required: false },
description: 'Duration of the animation',
defaultValue: 4500,
control: {
type: 'number',
},
},
},
parameters,
};
Expand All @@ -37,7 +47,7 @@ const Template = (args) => ({
showToast.value = true;
setTimeout(() => {
showToast.value = false;
}, 8000);
}, args.timeout + CSS_ANIMATION_TIME_MS);
},
args,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { VtmnAlertVariant } from './types';
import clsx from 'clsx';
import { VtmnButton } from '../../actions/VtmnButton';

const INFINITE_TIMEOUT_MS = 9999000;

export interface VtmnAlertProps
extends Omit<React.ComponentPropsWithoutRef<'dialog'>, 'onClose'> {
/**
Expand Down Expand Up @@ -48,14 +50,20 @@ export const VtmnAlert = ({
timeout = 8000,
className,
}: VtmnAlertProps) => {
const propertyStyle: Record<string, string> = {
'--vtmn-animation_alert-duration': `${
timeout < Infinity ? timeout : INFINITE_TIMEOUT_MS
}ms`,
};
return (
<div
aria-label={title}
role="dialog"
style={propertyStyle}
className={clsx(
'vtmn-alert',
`vtmn-alert_variant--${variant}`,
timeout > 0 && 'show',
timeout > 0 && 'show animate-delay',
className,
)}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import '@vtmn/css-snackbar/dist/index-with-vars.css';
import clsx from 'clsx';
import { VtmnButton } from '../../actions/VtmnButton';

const INFINITE_TIMEOUT_MS = 9999000;

export interface VtmnSnackbarProps
extends Omit<React.ComponentPropsWithoutRef<'dialog'>, 'onClose'> {
/**
Expand All @@ -20,6 +22,11 @@ export interface VtmnSnackbarProps
*/
withCloseButton?: boolean;

/**
* Duration of the snackbar in ms
*/
timeout?: number;

/**
* The alert callback close function
* @type {function}
Expand All @@ -33,11 +40,22 @@ export const VtmnSnackbar = ({
withCloseButton = false,
onClose,
className,
timeout = 4500,
}: VtmnSnackbarProps) => {
const propertyStyle: Record<string, string> = {
'--vtmn-animation_overlay-duration': `${
timeout < Infinity ? timeout : INFINITE_TIMEOUT_MS
}ms`,
};
return (
<div
role="status"
className={clsx('vtmn-snackbar', 'show', className)}
style={propertyStyle}
className={clsx(
'vtmn-snackbar',
timeout > 0 && 'show animate-delay',
className,
)}
onClick={onClose}
>
<div className="vtmn-snackbar_content">{content}</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import '@vtmn/css-toast/dist/index-with-vars.css';
import clsx from 'clsx';
import { VtmnButton } from '../../actions/VtmnButton';

const INFINITE_TIMEOUT_MS = 9999000;

export interface VtmnToastProps
extends Omit<React.ComponentPropsWithoutRef<'dialog'>, 'onClose'> {
/**
Expand All @@ -20,6 +22,11 @@ export interface VtmnToastProps
*/
withCloseButton?: boolean;

/**
* Duration of the toast in ms
*/
timeout?: number;

/**
* The alert callback close function
* @type {function}
Expand All @@ -33,13 +40,20 @@ export const VtmnToast = ({
withCloseButton = false,
onClose,
className,
timeout = 4500,
}: VtmnToastProps) => {
const propertyStyle: Record<string, string> = {
'--vtmn-animation_overlay-duration': `${
timeout < Infinity ? timeout : INFINITE_TIMEOUT_MS
}ms`,
};
return (
<div
role="status"
style={propertyStyle}
className={clsx(
'vtmn-toast',
'show',
timeout > 0 && 'show animate-delay',
withIcon && 'vtmn-toast--with-icon-info',
className,
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { computed, defineComponent, PropType, reactive } from 'vue';
import { VtmnAlertVariant } from './types';
import { VtmnButton } from '../../index';
const INFINITE_TIMEOUT_MS = 9999000;
export default /*#__PURE__*/ defineComponent({
name: 'VtmnAlert',
components: { VtmnButton },
Expand Down Expand Up @@ -49,17 +51,30 @@ export default /*#__PURE__*/ defineComponent({
return {
classes: computed(() => ({
'vtmn-alert': true,
show: props.timeout > 0,
'show animate-delay': props.timeout > 0,
[`vtmn-alert_variant--${props.variant}`]: props.variant,
})),
style: {
'--vtmn-animation_alert-duration': `${
typeof props.timeout === 'number' && props.timeout < Infinity
? props.timeout
: INFINITE_TIMEOUT_MS
}ms`,
},
handleClose,
};
},
});
</script>

<template>
<div :class="classes" role="alert" tabindex="-1" v-bind="$attrs">
<div
:class="classes"
:style="style"
role="alert"
tabindex="-1"
v-bind="$attrs"
>
<div class="vtmn-alert_content" role="document">
<div class="vtmn-alert_content-title">
{{ title }}
Expand Down
Loading

0 comments on commit 0fb0d05

Please sign in to comment.