123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <main class="[ container ] [ flow ]">
  <h1>Buy this thing!</h1>
  <p>It is very good and there's absolutely no hidden exceptions!</p>
  <a href="//example.com" class="button">Buy it now!!</a>
  <div class="terms">
    <disclosure-toggle button-label="See the terms and conditions" class="flow">
      <p>Here's some secret terms and conditions that we didn't want you to see because they explain how the product isn't actually very good. </p>
      <p>
        Lorem ipsum dolor sit amet, appareat pertinax et ius, ne pro nibh consulatu consetetur, nulla virtute definitiones nec in. Ad consul feugait eligendi mea, mutat tamquam ei mei. No hinc graecis phaedrum pro, cu erat ipsum sed, ut novum dissentiunt
        ullamcorper pro. <a href="#">Dicat aliquid dissentias</a> in per, meis alterum quaestio mei eu, vero praesent ex eam. Ei nam homero noluisse dissentiunt, ut vim quot putent. Vis elitr accusam accommodare id, cu usu quaestio conceptam, habeo tibique placerat eos
        cu. An nullam corpora consulatu qui, graeci euripidis est et.
      </p>
      <p>
        In tantas scripta nominati quo, ne essent maluisset voluptaria nam. Dicat putent feugiat ei sed, te vis delicata gubergren honestatis, ius liber blandit delicata ut. Vix et dictas detracto voluptua, vis an inani dicunt. Qui inciderint intellegebat ea,
        cetero verear cu duo.
      </p>
      <p>
        Vix id etiam sapientem. Vix case velit feugait eu. Id diceret delenit perpetua vis, has sale utamur aeterno te. Cum et consetetur mediocritatem.
      </p>
    </disclosure-toggle>
  </div>
</main>
            
          
!
            
              /* General presentation styles */

html {
  background: #212d40;
}

body,
.button,
.link-button {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
    Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}

body {
  padding: 1.5rem;
  line-height: 1.4;
  color: #f3f3f3;
}

p {
  margin: 0;
}

article > * + * {
  margin-top: 1.5em;
}

a:not([class]) {
  color: currentcolor;
}

a:not([class]):hover,
a:not([class]):focus {
  text-decoration: none;
}

.container {
  max-width: 30rem;
  margin: 0 auto;
}

.terms {
  --flow-space: 2rem;
}

/* Flow utility, which I write about here: https://24ways.org/2018/managing-flow-and-rhythm-with-css-custom-properties/ */
.flow {
  --flow-space: 1em;
}

.flow > * + * {
  margin-top: var(--flow-space);
}

/* Button component */
.button {
  display: inline-block;
  border: none;
  padding: 0.6rem 1.2rem 0.8rem 1.2rem;
  text-decoration: none;
  background: #98b06f;
  color: #212d40;
  font-size: 1.2rem;
  font-weight: 700;
  cursor: pointer;
  text-align: center;
  transition: background 250ms ease-in-out, transform 150ms ease;
  -webkit-appearance: none;
  -moz-appearance: none;
}

.button:hover,
.button:focus {
  background: #ffffff;
}

.button:focus {
  outline: 1px solid #212d40;
  outline-offset: -4px;
}

.button:active {
  transform: scale(0.99);
}
            
          
!
            
              console.clear();

class DisclosureToggle extends HTMLElement {
  constructor() {
    super();
    
    // Creates a shadow root for our slot etc
    this.root = this.attachShadow({mode: 'open'});
    
    // A key for setting the CSS Custom Property
    this.maxHeightKey = '--panel-max-height';
    
    // Create some component-level state
    this.state = {
      triggerExpanded: 'collapsed'
    }
  }
  
  // Render the component's markup and grab interactive elements
  connectedCallback() {
    this.root.innerHTML = this.render();
    
    this.trigger = this.root.querySelector('.js-trigger');
    this.panel = this.root.querySelector('.js-panel');
    this.panelInner = this.root.querySelector('.js-panel__inner');
    
    // Now the elements are grabbed, we can bind events and trigger the runner
    this.trigger.addEventListener('click', () => this.run());
    
    this.run();
  }
  
  // Looks for content within the panel and calculates its height. This makes the transition smoother.
  // Returns a promise so it can do its job before the toggle starts
  setMaxHeight() {
    const self = this;
    
    return new Promise((resolve, reject) => {
      if(!self.panel || !self.panelInner) {
        reject();
      }  

      self.panel.style.setProperty(self.maxHeightKey, `${self.panelInner.getBoundingClientRect().height}px`);
      resolve();
    });
  };
  
  // Sets the relevant aria role based on its current value. Sets it to false by default
  // if no value is set
  setTriggerState() {
    let currentExpandedState = this.trigger.getAttribute('aria-expanded');

    // If it's not set yet, set it to true so it is reversed below
    if(currentExpandedState === null) {
      currentExpandedState = 'true';
    }

    // Update our global state so other functions know what's up
    this.state.triggerExpanded = currentExpandedState === 'true' ? 'false' : 'true'

    this.trigger.setAttribute('aria-expanded', this.state.triggerExpanded);
  };
  
  // A handy runner that is called from multiple places
  run() {
    this.setMaxHeight()
      .then(() => this.setTriggerState());
  }
  
  // Getters are great and we use them to create a nice variable
  // for our button label text with a fallback
  get buttonLabel() {
    return this.getAttribute('button-label') || 'Toggle content';
  }
  
  render() {
    return `
    <style>
      hr {
        border: none;
        border-top: 1px dashed #ccc;
        margin: 1.5rem 0;
      }

      .link-button {
        display: inline-flex;
        align-items: center;
        text-decoration: underline;
        text-decoration-skip-ink: auto;
        background: transparent;
        padding: 0;
        margin: 0;
        border: 0;
        color: #98b06f;
        font-size: 1rem;
        cursor: pointer;
        -webkit-appearance: none;
        -moz-appearance: none;
      }


      .link-button svg {
        opacity: 0.8;
        margin-left: 0.5rem;
        transition: all 250ms ease-in-out;
        font-size: 0.8rem;
      }

      .link-button[aria-expanded="true"] svg {
        transform: rotate(-180deg);
      }

      .link-button:hover,
      .link-button:focus {
        text-decoration: none;
      }

      .link-button:focus {
        text-decoration: none;
        outline: 1px solid rgba(255, 255, 255, 0.4);
        outline-offset: 0.6rem;
      }

      .link-button:active {
        transform: scale(0.99);
      }

      .panel {
        --panel-max-height: 500px;

        transition: all 200ms ease;
        position: relative;
        overflow-y: auto;
        overflow-x: hidden;
        visibility: hidden;
        max-height: 0;
        -webkit-overflow-scrolling: touch;
      }

      .panel__inner {
        transition: all 500ms ease;
        transition-delay: 50ms;
        opacity: 0;
        transform: translateY(1rem);
        padding-top: 1.5rem;
      }

      [aria-expanded="true"] + .panel {
        max-height: var(--panel-max-height);
        visibility: visible;
      }

      [aria-expanded="true"] + .panel .panel__inner {
        opacity: 1;
        transform: translateY(0);
      }
    </style>
    <button class="[ link-button ] [ js-trigger ]" type="button">
      ${this.buttonLabel}
      <svg viewBox="0 0 512 512" aria-hidden="true" fill="currentColor" width="1em" height="1em">
        <path d="M60 99.333l196 196 196-196 60 60-256 256-256-256z"></path>
      </svg>
    </button>
    <div class="[ panel ] [ js-panel ]">
      <article class="[ panel__inner ] [ js-panel__inner ]">
        <slot></slot>
      </article>
    </div>
    `;
  }
}

if ('customElements' in window) {
  customElements.define('disclosure-toggle', DisclosureToggle);
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console