Skip to content

Instantly share code, notes, and snippets.

@bdanin
Last active June 5, 2019 03:55
Show Gist options
  • Save bdanin/76bb826012bad5234f7d6715d1cde8fc to your computer and use it in GitHub Desktop.
Save bdanin/76bb826012bad5234f7d6715d1cde8fc to your computer and use it in GitHub Desktop.
Adding JS to Drupal 8 Theme with .once()
name: My Theme
type: theme
base theme: adminimal_theme
description: 'Admin theme, sub-theme of Adminimal'
core: 8.x
libraries:
- my_theme/theme_base
(function ($) {
Drupal.behaviors.my_theme= {
attach: function (context, settings) {
// create a toggle to show hidden display:none content
$('#selector-id', context).once('my_theme').click(function () {
$('.hidden-object--show').slideToggle();
$(this).toggleClass('active');
});
// without an interactive toggle, show the hidden content
if (!$('#selector-id').length > 0) {
$('.hidden-object--show').show();
}
}
};
})(jQuery);
theme_base:
version: VERSION
js:
my_theme.js: {}
dependencies:
- core/jquery
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment