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
25 changes: 25 additions & 0 deletions src/sileo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ interface SileoProps {
canExpand?: boolean;
interruptKey?: string;
refreshKey?: string;
closeButton?: boolean;
onMouseEnter?: MouseEventHandler<HTMLButtonElement>;
onMouseLeave?: MouseEventHandler<HTMLButtonElement>;
onDismiss?: () => void;
Expand Down Expand Up @@ -133,6 +134,7 @@ export const Sileo = memo(function Sileo({
canExpand,
interruptKey,
refreshKey,
closeButton = false,
onMouseEnter,
onMouseLeave,
onDismiss,
Expand Down Expand Up @@ -652,6 +654,29 @@ export const Sileo = memo(function Sileo({
</div>
</div>

{closeButton && onDismiss && (
<div
data-sileo-close
role="button"
tabIndex={0}
aria-label="Close notification"
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
onDismiss();
}}
onKeyDown={(e) => {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
e.stopPropagation();
onDismiss();
}
}}
>
<X />
</div>
)}

{hasDesc && (
<div data-sileo-content data-edge={expand} data-visible={open}>
<div
Expand Down
30 changes: 30 additions & 0 deletions src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,36 @@
}
}

/* ----------------------------- Close Button ------------------------------ */

[data-sileo-close] {
position: absolute;
top: 0;
right: 0;
z-index: 30;
display: flex;
align-items: center;
justify-content: center;
width: var(--sileo-height);
height: var(--sileo-height);
padding: 0;
border: 0;
background: transparent;
cursor: pointer;
pointer-events: auto;
color: rgba(0, 0, 0, 0.4);
opacity: 0;
transition: opacity 150ms ease;
}

[data-sileo-toast]:hover [data-sileo-close] {
opacity: 1;
}

[data-sileo-close]:hover {
color: rgba(0, 0, 0, 0.7);
}

@media (prefers-reduced-motion: reduce) {
*,
*::before,
Expand Down
3 changes: 3 additions & 0 deletions src/toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface SileoToasterProps {
position?: SileoPosition;
offset?: SileoOffsetValue | SileoOffsetConfig;
options?: Partial<SileoOptions>;
closeButton?: boolean;
}

/* ------------------------------ Global State ------------------------------ */
Expand Down Expand Up @@ -218,6 +219,7 @@ export function Toaster({
position = "top-right",
offset,
options,
closeButton = false,
}: SileoToasterProps) {
const [toasts, setToasts] = useState<SileoItem[]>(store.toasts);
const [activeId, setActiveId] = useState<string>();
Expand Down Expand Up @@ -423,6 +425,7 @@ export function Toaster({
onMouseEnter={h.enter}
onMouseLeave={h.leave}
onDismiss={h.dismiss}
closeButton={closeButton}
/>
);
})}
Expand Down