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

              
                <mi-mensaje casi-visible msj="Hola, soy un mensaje"></mi-mensaje>
              
            
!

CSS

              
                mi-mensaje {
  display: block;
  padding: 10px;
  border: 5px solid black;
  margin-bottom: 10px;
}
mi-mensaje[casi-visible] {
  border: none;
}
              
            
!

JS

              
                class MiMensaje extends HTMLElement {
  // propiedad para escuchar solo los atributos definidos en el arreglo usando
  // attributeChangedCallback(attrName, oldValue, newValue)
  static get observedAttributes() {
    return ['msj'];
  }
  
  // propieda 'msj' sincronizada con el atributo 'msj'
  get msj() {
    return this.getAttribute('msj');
  }
  
  set msj(msj) {
    this.setAttribute('msj', msj);
  }
  
  // Pintar el mensaje que se declara en el atributo 'msj'
  pintarMensaje(msj) {
    this.innerHTML = msj
  }

  constructor() {
    super();
    this.addEventListener('click', function(e) {
      alert('Click en mensaje');
    });
    console.log('constructor: Cuando el elemento es creado');
  }
  
  // callback cuando se inserta el elemento en el dom
  connectedCallback() {
    console.log('connectedCallback: Cuando el elemento es insertado en el documento');
  }
  
  disconnectedCallback() {
    alert('disconnected: Cuando el elemento es eliminado del documento')
  }
  
  adoptedCallback() {
   alert('adoptedCallback: Cuando el elemento es adoptado por otro documento');
  }
  
  // Cuando un atributo es modificado, solo son llamados los atributos observados,
  // Estos son los definidos en la propiedad observedAttributes
  attributeChangedCallback(attrName, oldVal, newVal) {
    console.log('attributeChangedCallback: Cuando cambia un atributo');
    if (attrName === 'msj') {
      this.pintarMensaje(newVal);  
    }
  }
}

customElements.define('mi-mensaje', MiMensaje);

let miMensaje = document.createElement('mi-mensaje')
miMensaje.msj = 'Otro mensaje';
document.body.appendChild(miMensaje);

// Tambien puedes crear un elemento con el operador new
let tercerMensaje = new MiMensaje();
tercerMensaje.msj = 'Tercer mensaje';
document.body.appendChild(tercerMensaje);
              
            
!
999px

Console