@shopify/app-bridge-core
Version:
**[Join our team and work on libraries like this one.](https://www.shopify.ca/careers)**
91 lines (88 loc) • 2.38 kB
JavaScript
import { actionWrapper, getMergedProps } from '../helper.js';
import { ActionSet } from '../ActionSet.js';
import { Group } from '../types.js';
var Action;
(function (Action) {
Action["SHOW"] = "APP::TOAST::SHOW";
Action["CLEAR"] = "APP::TOAST::CLEAR";
Action["ACTION"] = "APP::TOAST::ACTION";
})(Action || (Action = {}));
function show(toastMessage) {
return actionWrapper({
group: Group.Toast,
payload: toastMessage,
type: Action.SHOW,
});
}
function clear(payload) {
return actionWrapper({
payload,
group: Group.Toast,
type: Action.CLEAR,
});
}
function primaryAction(payload) {
return actionWrapper({
payload,
group: Group.Toast,
type: Action.ACTION,
});
}
class Toast extends ActionSet {
message = '';
duration = 5000;
isError;
action;
constructor(app, options) {
super(app, Group.Toast, Group.Toast);
this.set(options);
}
get options() {
return {
duration: this.duration,
isError: this.isError,
message: this.message,
action: this.action?.content
? {
content: this.action.content,
}
: undefined,
};
}
get payload() {
return {
id: this.id,
...this.options,
};
}
set(options) {
const mergedOptions = getMergedProps(this.options, options);
const { message, duration, isError, action } = mergedOptions;
this.message = message;
this.duration = duration;
this.isError = isError;
this.action = action?.content
? {
content: action.content || '',
}
: undefined;
return this;
}
dispatch(action) {
switch (action) {
case Action.SHOW: {
const openAction = show(this.payload);
this.app.dispatch(openAction);
break;
}
case Action.CLEAR:
this.app.dispatch(clear({ id: this.id }));
break;
case Action.ACTION:
this.app.dispatch(primaryAction({ id: this.id }));
break;
}
return this;
}
}
export { Action, Toast, clear, primaryAction, show };