Show a small alert box at any corner of screen.
Using web component.
Live demo link: https://www.test.salman2301.com/alert-web-component
There are different way we can use custom element in Wix Site. I am showing three different way to use it. The easy way is to use Wix Block with CDN and you can skip the below 2.
- Wix Block + CDN
- Public JS file
- CDN
Easy way is to use Custom elment in Wix site is by using with Wix Block. It provid some level of IDE support.
- Click on this link. Follow the step to Install the Wix Block on any Wix site.
- Drag and Drop the Widget or Wix Block.
- Change the element Id to
widgetAlertvia the properties panel. - Drag and Drop a Custom element under
Add Element/Embed/Custom Elementand change the Id toCustomAlertvia properties panel. - Click on the Custom element and select Change source.
- Select
Server URLpaste this URLhttps://cdn.jsdelivr.net/gh/salman2301/wix-alert-component@master/dist/index.js - Set the Tag Name to
alerts-component - Add the following Code in the page sections.
// page code.js
$w.onReady(function () {
// This step is needed to let the Wix Block select the Custom element
$w('#widgetAlert').setCustomElement($w('#CustomAlert'));
// This will show Warning Alert
$w('#widgetAlert').alert("This is a warning message." )
});
Note:
for the alert method, You can pass a string or object
// using string
$w('#widgetAlert').alert("This is a alert message." )
Is same as this
// using object
$w('#widgetAlert').alert({ message: "This is a alert message." })
You can also pass more options like title, type of alert, wait time, style using the object method
Checkout below attribute new alert
$w('#widgetAlert').alert({ title: "Hello", message: "Welcom to the site." })
Follow the steps below to setup web component in wix site using publicjs file.
- Open the editor
- Turn on dev mode
- Drag and drop Custom Element from '+' icon > More > Custom Element (in Custom Embed Section)
- Select 'Choose Source'
- Select 'Corvid file'
- Click on Select on of your file
- If you don't have any file, it will show 'Create a new file' it will create a folder custom element
- Open the
Public/custom-elementsin theSite Structureand create a new file, with a nameSnackbar.js - Copy the code from this repo dist/index.js
- Paste the code in the
Snackbar.jsfile - And then go to the page, where you drag and drop the custom element
- Change the Tag Name field to
alerts-component - Drag the element to (Header or Footer) or check "Show on all page" to set the element globally
you can skip 5-11 steps by selecting the server URL option after point 4,
file is hosted in jsdelivr
Paste the below link and continue from step 12
https://cdn.jsdelivr.net/gh/salman2301/wix-alert-component@master/dist/index.js
- setposition
- newalert
position demo:
Setposition allows you choose any corner of the website to show your alert. for desktop by default set to bottom-right and avaiable position are as follows
- top-left
- top-right
- bottom-left
- bottom-right
And for mobile
- top
- bottom
By default the alert will be center horizontally for mobile,
Responsive Demo:
// example wix code
$w.onReady(() => {
$w("#CustomElement1").setAttribute("setposition", "top-right");
});newalert takes a stringified Object with the message method.
Other methods are as follow
{
// Optional changed based on type by Defalt it's "Oops" which is for Error type. use this to override.
title : "Oops",
// required field, message display in the alert
message : "Alert message!.",
// set color of alert based on success
type:"success", // "error" | "info" | "warn"|"success"
// if it's false, user need to click on the close button
autoClose : true,
// you can also use any word e.g: "ACTION"| "Yes"|"No"
closeLabel : "X",
// wait for 8 sec before autoClose, if it's set to true
wait : 8,
// custom css style
style:"",
// callback function trigger on autoClose or button click
onClose: function(){},
// callback function trigger on button click
onAction: function(){},
// brandColor set any hex, rgb color, refeclt the border
brandColor:"#4BB543",
// redirect url on title or message clicked!
knowMoreUrl: "https://salman2301.com"
}// Using Wix Block
$w("widgetAlert").alert("hello world!");
// OR
// Using Custom element without Wix Block
$w("#CustomElement1").setAttribute(
"newalert",
JSON.stringify({ message: "hello world!" })
);/*
Alert with action
*/
let alertPrivacy = {
message: "We Use cookies to store information! Accept our privacy policy!",
autoClose: false,
closeLabel: "Accept",
onAction: handleAction,
};
function handleAction(e) {
/*
e => {
reason : reason, // action | timeout
message: message, // your message
id: id
}
*/
// save your cookie :)
}
// Using Wix Block
$w("widgetAlert").alert(alertPrivacy);
// Without Wix Block
$w("#CustomElement1").setAttribute("newalert", JSON.stringify(alertPrivacy));/*
on Dataset Error
*/
let alertError = {
message: "Something went wrong! Check the field and try again",
wait: 12, // wait for 12 sec
type: "error",
};
$w("#dataset").onReady(() => {
$w("#dataset").onError(() => {
// Using Wix Block
$w("widgetAlert").alert(alertError);
// Without Wix Block
$w("#CustomElement1").setAttribute("newalert", JSON.stringify(alertError));
});
});Version 2 has better design Update
- Ability to add link in message
- Allow html for message
- Generate the code, using wix site
- On Click of a message body, redirect to the url
- Callback also need to fire .on() the webcomponent
- Fix Animation on left side alerts
- Fix Animation Fly in and Fly out!
- Icon for type of message
- Refactor the code
- Demo link to test the component
- Hosted Server URL
- Clone this repo
- goto the cloned folder
- run
npm install - and
npm run dev - server should be running in port
5000
- svelte
MIT
Leave a message in my website.


