Skip to content

Instantly share code, notes, and snippets.

@jmakeig
Last active July 13, 2017 06:22
Show Gist options
  • Select an option

  • Save jmakeig/33faf575441a708e1120ae20d1afd0e3 to your computer and use it in GitHub Desktop.

Select an option

Save jmakeig/33faf575441a708e1120ae20d1afd0e3 to your computer and use it in GitHub Desktop.
Minimal HTML toggle group
<!DOCTYPE html>
<html>
<head>
<title>Toggle group</title>
<style type="text/css">
.toggleable:before {
width: 0.75rem;
/* The two arrows have different widths */
display: inline-block;
content: '▾';
}
.toggle-group {
/* Generally the same as .toggleable:before width */
margin-left: 0.75rem;
}
.toggleable.toggle-collapsed:before {
content: '▸';
}
.toggle-collapsed > .toggle-group {
display: none;
}
</style>
</head>
<body>
<div class="toggleable">
<span class="label">Stuff</span>
<div class="toggle-group">
<ol>
<li>A</li>
<li>B</li>
<li>C</li>
</ol>
<div class="toggleable">
<span class="label">Nested</span>
<div class="toggle-group">
<ol>
<li>i</li>
<li>ii</li>
<li>iii</li>
</ol>
</div>
</div>
<div class="toggleable">
<span class="label">Another nest</span>
<div class="toggle-group">
<ol>
<li>iv</li>
<li>v</li>
<li>vi</li>
</ol>
</div>
</div>
</div>
</div>
<script type="application/javascript">//<--
'use strict';
document.body.addEventListener('click', evt => {
if (evt.target && evt.target.matches('.toggleable')) {
evt.target.classList.toggle('toggle-collapsed');
}
});
//-->
</script>
</body>
</html>
@jmakeig

jmakeig commented Jul 3, 2017 •

Copy link
Copy Markdown
Author
  • Make sure this works on mobile
  • Separate .toggleable from the toggle affordance (i.e. ▸) (see Element.prototype.closest)
    This is going to be difficult to do cleanly (i.e. without introducing more mark-up) because :before pseudo-elements are not part of the DOM and thus can have events attached or even matched in a handler on the “parent”.
  • Move the toggle affordance to the left of the list item’s number/bullet
  • toggle-none is a terrible name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment