Last active
June 5, 2019 03:55
-
-
Save bdanin/76bb826012bad5234f7d6715d1cde8fc to your computer and use it in GitHub Desktop.
Adding JS to Drupal 8 Theme with .once()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: My Theme | |
type: theme | |
base theme: adminimal_theme | |
description: 'Admin theme, sub-theme of Adminimal' | |
core: 8.x | |
libraries: | |
- my_theme/theme_base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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