carbon-components-svelte/src/Notification/InlineNotification.svelte
Eric Liu 27ba4350cf
Align v10.48 (#907)
* chore(deps-dev): upgrade carbon-components to v10.48.0

* feat(ui-shell): support HeaderNavMenu with current item

* refactor(tile): remove unnecessary "position: relative" style

* refactor(tile): remove unused ref

* feat(inline-notification): make title/subtitle slottable

Closes #883

* feat(toast-notification): make title/subtitle/caption slottable

* chore(notification): deprecate NotificationTextDetails
2021-11-12 16:25:02 -08:00

99 lines
2.8 KiB
Svelte

<script>
/**
* @event {{ timeout: boolean }} close
*/
/**
* Specify the kind of notification
* @type {"error" | "info" | "info-square" | "success" | "warning" | "warning-alt"}
*/
export let kind = "error";
/** Set to `true` to use the low contrast variant */
export let lowContrast = false;
/** Set the timeout duration (ms) to hide the notification after opening it */
export let timeout = 0;
/** Set the `role` attribute */
export let role = "alert";
/** Specify the title text */
export let title = "";
/** Specify the subtitle text */
export let subtitle = "";
/** Set to `true` to hide the close button */
export let hideCloseButton = false;
/** Specify the ARIA label for the icon */
export let iconDescription = "Closes notification";
import { createEventDispatcher, onMount } from "svelte";
import NotificationIcon from "./NotificationIcon.svelte";
import NotificationButton from "./NotificationButton.svelte";
const dispatch = createEventDispatcher();
let open = true;
let timeoutId = undefined;
function close(closeFromTimeout) {
open = false;
dispatch("close", { timeout: closeFromTimeout === true });
}
onMount(() => {
if (timeout) {
timeoutId = setTimeout(() => close(true), timeout);
}
return () => {
clearTimeout(timeoutId);
};
});
</script>
<!-- svelte-ignore a11y-mouse-events-have-key-events -->
{#if open}
<div
role="{role}"
kind="{kind}"
class:bx--inline-notification="{true}"
class:bx--inline-notification--low-contrast="{lowContrast}"
class:bx--inline-notification--hide-close-button="{hideCloseButton}"
class:bx--inline-notification--error="{kind === 'error'}"
class:bx--inline-notification--info="{kind === 'info'}"
class:bx--inline-notification--info-square="{kind === 'info-square'}"
class:bx--inline-notification--success="{kind === 'success'}"
class:bx--inline-notification--warning="{kind === 'warning'}"
class:bx--inline-notification--warning-alt="{kind === 'warning-alt'}"
{...$$restProps}
on:click
on:mouseover
on:mouseenter
on:mouseleave
>
<div class:bx--inline-notification__details="{true}">
<NotificationIcon notificationType="inline" kind="{kind}" />
<div class:bx--inline-notification__text-wrapper="{true}">
<p class:bx--inline-notification__title="{true}">
<slot name="title">{title}</slot>
</p>
<div class:bx--inline-notification__subtitle="{true}">
<slot name="subtitle">{subtitle}</slot>
</div>
<slot />
</div>
</div>
<slot name="actions" />
{#if !hideCloseButton}
<NotificationButton
iconDescription="{iconDescription}"
notificationType="inline"
on:click="{close}"
/>
{/if}
</div>
{/if}