EC System

Dropdowns

Dropdown components contain elements that, when tapped, reveal additional content.

Implementation notes

If you plan on using multiple dropdown components on the same page, make sure you give them unique IDs to the component itself but also to the button component that it contains. Initialization can be acheived like this:

ECL.initExpandables('#news-expandable-button1');
ECL.initExpandables('#news-expandable-button2');
ECL.dropdown('#dropdown1');
ECL.dropdown('#dropdown2');

Resources

Menu Button General Information

Examples:

{% set _links = links|default([]) %}

<!-- Button dropdown -->
<div class="ecl-dropdown" id="dropdown1">
  {% include '@ecl/ec-component-button' with {
    'label': 'Create',
    'type_attribute': 'button',
    'extra_classes': 'ecl-expandable__button',
    'extra_attributes': [
      { 'name': 'aria-controls', 'value': 'example-button-dropdown1'},
      { 'name': 'aria-expanded', 'value': 'false'},
      { 'name': 'id', 'value': 'example-expandable-button1' },
    ]
  } %}
  {% include '@ecl/ec-component-link-block' with {
    'extra_classes': 'ecl-dropdown__body',
    'extra_attributes': [
      { 'name': 'id', 'value': 'example-button-dropdown1'},
      { 'name': 'aria-labelledby', 'value': 'example-expandable-button1'},
      { 'name': 'aria-hidden', 'value': 'true'},
    ],
    'links': _links,
  } %}
</div>
{
  "_demo": {
    "scripts": "\n      document.addEventListener('DOMContentLoaded', function () {\n        ECL.initExpandables('#example-expandable-button1');\n        ECL.dropdown('#dropdown1');\n      });\n    "
  },
  "links": [
    {
      "href": "#",
      "label": "Article"
    },
    {
      "href": "#",
      "label": "Page"
    },
    {
      "href": "#",
      "label": "Community"
    }
  ]
}
<!-- Button dropdown -->
<div class="ecl-dropdown" id="dropdown1">

  <button type="button" class="ecl-button ecl-button--default ecl-expandable__button" aria-controls="example-button-dropdown1" aria-expanded="false" id="example-expandable-button1">Create</button>

  <div class="ecl-link-block ecl-dropdown__body" id="example-button-dropdown1" aria-labelledby="example-expandable-button1" aria-hidden="true">
    <ul class="ecl-link-block__list">
      <li class="ecl-link-block__item">

        <a class="ecl-link ecl-link--standalone ecl-link-block__link" href="#">Article</a>
      </li>
      <li class="ecl-link-block__item">

        <a class="ecl-link ecl-link--standalone ecl-link-block__link" href="#">Page</a>
      </li>
      <li class="ecl-link-block__item">

        <a class="ecl-link ecl-link--standalone ecl-link-block__link" href="#">Community</a>
      </li>
    </ul>
  </div>
</div>
  • Content:
    /**
     * `Node#contains()` polyfill.
     *
     * See: http://compatibility.shwups-cms.ch/en/polyfills/?&id=1
     *
     * @param {Node} node
     * @param {Node} other
     * @return {Boolean}
     * @public
     */
    
    function contains(node, other) {
      // eslint-disable-next-line no-bitwise
      return node === other || !!(node.compareDocumentPosition(other) & 16);
    }
    
    export const dropdown = selector => {
      const dropdownsArray = Array.prototype.slice.call(
        document.querySelectorAll(selector)
      );
    
      document.addEventListener('click', event => {
        dropdownsArray.forEach(dropdownSelection => {
          const isInside = contains(dropdownSelection, event.target);
    
          if (!isInside) {
            const dropdownButton = document.querySelector(
              `${selector} > [aria-expanded=true]`
            );
            const dropdownBody = document.querySelector(
              `${selector} > [aria-hidden=false]`
            );
            // If the body of the dropdown is visible, then toggle.
            if (dropdownBody) {
              dropdownButton.setAttribute('aria-expanded', false);
              dropdownBody.setAttribute('aria-hidden', true);
            }
          }
        });
      });
    };
    
    export default dropdown;
    
  • URL: /components/raw/ec-component-dropdown/ec-component-dropdown.js
  • Filesystem Path: ../../src/systems/ec/ec-component/ec-component-dropdown/ec-component-dropdown.js
  • Size: 1.2 KB
  • Content:
    /**
     * ECL Dropdowns
     * @define dropdown
     */
    
    // Import base and generic
    @import '@ecl/ec-base/ec-base';
    @import '@ecl/generic-component-dropdown/generic-component-dropdown';
    
    // Check if overridden dependencies are already loaded, if needed
    @include check-imports(('ec-component-button', 'ec-component-link-block'));
    
    // Use generic mixin
    @include exports('ec-component-dropdown') {
      @include ecl-dropdown();
    }
    
  • URL: /components/raw/ec-component-dropdown/ec-component-dropdown.scss
  • Filesystem Path: ../../src/systems/ec/ec-component/ec-component-dropdown/ec-component-dropdown.scss
  • Size: 413 Bytes
  • Handle: @ecl/ec-component-dropdown
  • Tags: molecule
  • Preview:
  • Filesystem Path: ../../src/systems/ec/ec-component/ec-component-dropdown/ec-component-dropdown.twig