Skip to content

Comments

ToastService: Add default options & methods for setting/getting default options#8384

Open
pilotkid wants to merge 1 commit intoprimefaces:masterfrom
pilotkid:feat-toast-default-message-options
Open

ToastService: Add default options & methods for setting/getting default options#8384
pilotkid wants to merge 1 commit intoprimefaces:masterfrom
pilotkid:feat-toast-default-message-options

Conversation

@pilotkid
Copy link

@pilotkid pilotkid commented Jan 16, 2026

Add default options support to ToastService

This PR adds support for default toast options, reducing repetition (e.g. always setting life) while remaining fully backwards compatible.

Documentation has been updated to demonstrate usage.

Impact

  • Fully backwards compatible
  • Minimal internal change
  • Improved developer experience
  • Documentation updated ToastServiceDoc.vue

Why

Today users must repeat common options on every toast:

toast.add({ severity: 'success', summary: 'Saved', life: 5000 });
toast.add({ severity: 'error', summary: 'Failed', life: 5000 });

This is repetitive and makes global consistency harder.

What’s changed

Install-time defaults

You can now pass defaults (supports all toastMessage parameters) when installing the service:

app.use(ToastService, {
  life: 5000
});

Note: If no options object is provided, the service behaves exactly as before.

Runtime APIs

Two new methods are available on the toast service:

setOptions(opts: ToastServiceOptions)

Merges the provided options into the current defaults (shallow merge). Useful for dynamically adjusting behavior (for example: changing toast duration based on end user's preferences).

getOptions(): ToastServiceOptions

Returns the current default options.


Merge behavior

Each toast message is shallow-merged with defaults:

Object.assign({}, defaults, message);
  • No breaking behavior
  • Defaults apply automatically
  • Message-level values always override defaults

Old vs New Usage

Before (still supported)

app.use(ToastService);

toast.add({
  severity: 'success',
  summary: 'Saved',
  life: 5000
});

After (with defaults)

app.use(ToastService, { life: 5000 });

toast.add({
  severity: 'success',
  summary: 'Saved'
});

Sorry for the longer description. I understand this may be considered a feature request, but it’s something I care about and believe offers a real DX improvement with very minimal impact. I added extra context to make the intent and scope clear, in case that helps with review or inspires a similar implementation by the core team.

@dzimmerman902
Copy link

dzimmerman902 commented Jan 26, 2026

I like this idea. I find it very helpful to minimize the amount of repeated code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants