Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add closeButton property to toast options #17

Merged
merged 2 commits into from
Apr 16, 2024
Merged
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
7 changes: 5 additions & 2 deletions libs/ngx-sonner/src/lib/toast.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const defaultClasses: ToastClassnames = {
(pointerdown)="onPointerDown($event)"
(pointerup)="onPointerUp()"
(pointermove)="onPointerMove($event)">
@if (toast().dismissible && closeButton() && !toast().component) {
@if (closeButton() && !toast().component) {
<button
aria-label="Close toast"
[attr.data-disabled]="disabled()"
Expand Down Expand Up @@ -219,7 +219,9 @@ export class ToastComponent implements AfterViewInit, OnDestroy {
position = input.required<ToastProps['position']>();
visibleToasts = input.required<ToastProps['visibleToasts']>();
expandByDefault = input.required<ToastProps['expandByDefault']>();
closeButton = input.required<ToastProps['closeButton']>();
_closeButton = input.required<ToastProps['closeButton']>({
alias: 'closeButton',
});
interacting = input.required<ToastProps['interacting']>();
cancelButtonStyle = input<ToastProps['cancelButtonStyle']>();
actionButtonStyle = input<ToastProps['actionButtonStyle']>();
Expand Down Expand Up @@ -268,6 +270,7 @@ export class ToastComponent implements AfterViewInit, OnDestroy {
}, 0)
);
invert = computed(() => this.toast().invert ?? this._invert());
closeButton = computed(() => this.toast().closeButton ?? this._closeButton());
disabled = computed(() => this.toastType() === 'loading');

timeoutId: ReturnType<typeof setTimeout> | undefined;
Expand Down
11 changes: 6 additions & 5 deletions libs/ngx-sonner/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ export type ToastT = {
component?: Type<unknown>;
componentProps?: Record<string, unknown>;
invert?: boolean;
closeButton?: boolean;
dismissible?: boolean;
description?: string | Type<unknown>;
cancelButtonStyle?: string;
actionButtonStyle?: string;
duration?: number;
delete?: boolean;
important?: boolean;
Expand All @@ -51,16 +51,17 @@ export type ToastT = {
};
onDismiss?: (toast: ToastT) => void;
onAutoClose?: (toast: ToastT) => void;
dismissible?: boolean;
promise?: PromiseT;
cancelButtonStyle?: string;
actionButtonStyle?: string;
style?: Record<string, unknown>;
unstyled?: boolean;
class?: string;
classes?: ToastClassnames;
descriptionClass?: string;
position?: Position;
unstyled?: boolean;
/**
* @internal This is used to determine if the toast has been updated to determine when to reset timer. Hacky but works.
* @internal This is used to determine if the toast has been updated to determine when to reset timer.
*/
updated?: boolean;
};
Expand Down
15 changes: 13 additions & 2 deletions libs/ngx-sonner/src/tests/toaster.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,25 @@ describe('Toaster', () => {
expect(closeButton).not.toBeNull();
});

it('should not show close button if the toast is not dismissible', async () => {
it('should not show close button if the toast has closeButton false', async () => {
const { user, trigger, container } = await setup({
callback: toast => toast('Hello world', { dismissible: false }),
callback: toast => toast('Hello world', { closeButton: false }),
closeButton: true,
});

await user.click(trigger);
const closeButton = container.querySelector('[data-close-button]');
expect(closeButton).toBeNull();
});

it('should show close button if the toast has closeButton true', async () => {
const { user, trigger, container } = await setup({
callback: toast => toast('Hello world', { closeButton: true }),
closeButton: false,
});

await user.click(trigger);
const closeButton = container.querySelector('[data-close-button]');
expect(closeButton).not.toBeNull();
});
});