Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Build a Better Accordion

Build a Better Accordion

Accordions are a useful way to handle groups of nested lists when there’s not a lot of screen real estate to play with – especially handy for off-canvas navigation on mobile devices. Don’t see yourself needing to build an accordion any time soon? That’s okay! We’ll talk about things that’ll help make anything you build even more awesome:

• Semantic markup (When do I use an “<a>”? When do I use a “<button>”?)
• Animatable and non-animatable CSS properties
• Making Sass do the work for you

After laying the groundwork, I’ll walk through building a mixin that lets us add good user affordance and silky-smooth CSS animations with a single line of code.

This talk is from ATXSass on 23 July 2015 – Here’s the video.

Nick Piesco

July 23, 2015
Tweet

More Decks by Nick Piesco

Other Decks in Programming

Transcript

  1. <ul class=”league-list”> <ul class=”division-list”> <ul class=”division-list”> <ul class=”team-list”> <ul class=”team-list”>

    <ul class=”team-list”> <ul class=”team-list”> <ul class=”team-list”> <ul class=”team-list”>
  2. <nav class=”main-navigation”> <ul class=”league-list”> <li class=”league”> American League <ul class=”division-list”>

    <li class=”division”> East <ul class=”team-list”> <li class=”team”> <a href=”orioles.html”>Baltimore Orioles</a> </li> [ . . . ] </ul> [ . . . ] </li> [ . . . ] </ul> [ . . . ] </li> [ . . . ] </ul> </nav>
  3. <li class=”division”> <button class=”list-toggle js-list-toggle”> East </button> <ul class=”team-list”> [

    . . . ] * Separate presentational classes from JS classes to preserve future developer sanity *
  4. <li class=”division”> <button class=”list-toggle js-list-toggle”> East </button> <ul class=”team-list”> <li

    class=”team”> <a href=”orioles.html”>Baltimore Orioles</a> </li> [ . . . ]
  5. <li class=”division”> <button class=”list-toggle js-list-toggle”> East </button> <ul class=”team-list”> <li

    class=”team”> <a href=”orioles.html”>Baltimore Orioles</a> </li> [ . . . ] Goes somewhere Does something
  6. Animatable CSS properties • color and opacity • transform •

    margin and padding • positioning • height and width https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties
  7. The browser runs the entire animation, regardless of where the

    actual content stops * 8675309em ~ 23 miles
  8. .league-list, .division-list, .team-list { max-height: 0; transition: max-height 0.5s ease;

    &.open { max-height: 100vh; } } 1vh/vw = 1% of viewport height/width
  9. $('.js-list-toggle').click(function() { $(this).toggleClass('list-open'); }); .list-toggle { position: relative; &:after {

    content: '\25bc'; // Down arrow position: absolute; right: 0.5em; &.list-open { &:after { content: '\25b2'; // Up arrow } } }
  10. $('.js-list-toggle').click(function() { $(this).toggleClass('list-open'); }); .list-toggle { position: relative; &:after {

    content: '\25bc'; // Down arrow position: absolute; right: 0.5em; transition: transform 0.5s ease; &.list-open { &:after { transform: rotateX(180deg); } } }
  11. h2 + p { [ . . . ] }

    <section> <h2>I’m a heading!</h2> <p>I’m selected!</p> <p>...but I’m not. :(</p> </section> Adjacent sibling selector (+)
  12. .list-toggle { [ . . . ] & + ul

    { max-height: 0; transition: max-height 0.5s ease; } [ . . . ] &.list-open + ul { max-height: 100vh; } }
  13. @mixin accordion { position: relative; &:after { content: '\25bc'; position:

    absolute; right: 0.5em; transition: transform 0.5s ease; } & + ul { max-height: 0; overflow: hidden; transition: max-height 0.5s ease; } &.list-open { &:after { transform: rotateX(180deg); } & + ul { max-height: 100vh; } } } .list-toggle { @include accordion; }
  14. @mixin accordion( $duration, $easing ) { [ . . .

    ] &:after { [ . . . ] transition: transform $duration $easing; } & + ul { [ . . . ] transition: max-height $duration $easing; } [ . . . ] } .list-toggle { @include accordion(0.3s, ease-out); }
  15. @mixin accordion( $duration: 0.5s, $easing: ease ) { [ .

    . . ] &:after { [ . . . ] transition: transform $duration $easing; } & + ul { [ . . . ] transition: max-height $duration $easing; } [ . . . ] } .list-toggle { @include accordion(0.3s, ease-out); }
  16. • Start with semantic markup • Use JS to toggle

    classes for silky-smooth CSS animations • Let Sass do the repetitive work for you
  17. Thanks! @nickpiesco John Linnell: tmbgareok.tumblr.com PNC Park: Minorfreak/Wikimedia Commons Weird

    Al Yankovic: Greg Grudt/Mathew Imaging Lawrence Welk: djworksmusic.com