Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <greeting-message></greeting-message>
              
            
!

CSS

              
                
              
            
!

JS

              
                // Extend the HTMLElement class to create the web component
class GreetingMessage extends HTMLElement {

	/**
	 * The class constructor object
	 */
	constructor () {

		// Always call super first in constructor
		super();

		// Render HTML
		let btnText = this.innerHTML.trim();
		this.innerHTML =
			`<p>
				<button>${this.hasAttribute('wave') ? '👋' : ''} ${btnText ? btnText : 'Hi there!'}</button>
			</p>
			<div class="message" aria-live="polite"></div>`;

	}

	/**
	 * Handle click events
	 * @param  {Event} event The event object
	 */
	clickHandler (event) {

		// Get the host component
		let host = event.target.closest('greeting-message');

		// Get the message element
		let target = host.querySelector('.message');
		if (!target) return;

		// Inject the message into the UI
		let name = host.getAttribute('name');
		target.textContent = `Hi there, ${name ? name : 'friend'}! Hope you're having a great day!`;

		// Clear the message after 5 seconds
		setTimeout(function () {
			target.textContent = '';
		}, 5000);

	}

	/**
	 * Runs each time the element is appended to or moved in the DOM
	 */
	connectedCallback () {

		// Attach a click event listener to the button
		let btn = this.querySelector('button');
		if (!btn) return;
		btn.addEventListener('click', this.clickHandler);

	}

	/**
	 * Runs when the element is removed from the DOM
	 */
	disconnectedCallback () {

		// Remove the click event listener from the button
		let btn = this.querySelector('button');
		if (!btn) return;
		btn.removeEventListener('click', this.clickHandler);

	}

	/**
	 * Create a list of attributes to observe
	 */
	static get observedAttributes () {
		return ['logout'];
	}

	/**
	 * Runs when the value of an attribute is changed on the component
	 * @param  {String} name     The attribute name
	 * @param  {String} oldValue The old attribute value
	 * @param  {String} newValue The new attribute value
	 */
	attributeChangedCallback (name, oldValue, newValue) {

		// console.log('changed', name, oldValue, newValue, this);

		// Remove the button
		let btn = this.querySelector('button');
		if (btn) {
			btn.removeEventListener('click', this.clickHandler);
			btn.remove();
		}

		// Get the message element
		let target = this.querySelector('.message');
		if (target) {

			// Inject the message into the UI
			let name = this.getAttribute('name');
			target.textContent = `Bye, ${name ? name : 'friend'}! See you next time.`;

		}

	}

}

// Define the new web component
if ('customElements' in window) {
	customElements.define('greeting-message', GreetingMessage);
}


let greeting = document.querySelector('greeting-message');

// Nothing will happen here, because we're not watching this attribute
greeting.setAttribute('hello', 'you');

// logs "changed", "logout", null, "true"
setTimeout(function () {
    greeting.setAttribute('logout', true);
}, 5000);
              
            
!
999px

Console