-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Toggling classes on click #1032
Comments
@cramforce Do you think this is the same as #1017? |
This will be possible ? |
Have we got a timeline for this? Would love to have a show/hide boilerplate button for http://amp-by-example.appspot.com. |
We got according now that could be used for this, I think. /cc @sriramkrish85 |
@sebastianbenz |
@sriramkrish85 @dvoytenko Works splendid! When is it going to be released? |
@sriramkrish85 @dvoytenko FYI https://amp-by-example.appspot.com/amp-accordion.html |
@sebastianbenz I'll leave it to @rudygalfi to state the dates. |
We need to get the validation in. @ericlindley-g is going to track that down with @Gregable. |
@ericlindley-g @choumx To resolve if this is covered by bind, could be folded into bind, or should be closed. |
Yep—covered by bind. Closing for now, but folks should feel free to re-open if there's a need for a simpler syntax, as proposed above. |
@ericlindley-g I think more is needed here. In particular, what if there are multiple variable classes on an element already (such as those added to If bind were able to reference the existing state that could be a solution. So maybe something like this, with WP template code to show intended usage:
It would be important for
So perhaps it is better to go the action route as initially proposed. <body id="body" <?php body_class(); ?>>
<button on="tap:body.toggleClass('no-scroll')">Toggle menu</button> There should probably be |
Good point, @westonruter — reopening and /cc @choumx for visibility |
A bit clunky, but you could store the default classes in an <body [class]="toggleVariable
? defaultBodyClasses.concat('no-scroll')
: defaultBodyClasses"> But yea, this sounds a lot like #10622. And @sebastianbenz's suggestion in particular: #10622 (comment)
|
Here's how I ended up implementing a state-based toggle on the <body <?php body_class(); ?> [class]="navMenu.expanded ? <?php echo esc_attr( wp_json_encode( get_body_class( 'sidebar-open' ) ) ) ?> : <?php echo esc_attr( wp_json_encode( get_body_class() ) ); ?>"> Importantly, this only would allow for toggling a single class. It's also not very pretty. |
In the WordPress theme there is a button that invokes jQuery to <button class="menu-toggle" aria-controls="primary-menu" aria-expanded="false"><?php esc_html_e( 'Primary Menu', '_s' ); ?></button>
<amp-state id="siteNavigationMenu">
<script type="application/json">
{
"expanded": false
}
</script>
</amp-state>
<input
id="site-navigation-expanded"
type="checkbox"
hidden
on="change:AMP.setState( { siteNavigationMenu: { expanded: event.checked } } )"
>
<label
class="menu-toggle"
for="site-navigation-expanded"
tabindex="0"
role="button"
aria-controls="primary-menu"
aria-expanded="false"
[aria-expanded]="siteNavigationMenu.expanded ? 'true' : 'false'"
>
<?php esc_html_e( 'Primary Menu', '_s' ); ?>
</label>
<button
class="menu-toggle"
aria-controls="primary-menu"
aria-expanded="false"
[aria-expanded]="siteNavigationMenu.expanded ? 'true' : 'false'"
on="change:AMP.toggleState( 'siteNavigationMenu', 'expanded' )"
>
<?php esc_html_e( 'Primary Menu', '_s' ); ?>
</button>
|
Interesting problem. Here is another approach that supports toggling multiple
Full example: https://jsbin.com/qilalovife/edit?html,output. It's still not very nice, but solves the problem. It'd be nicer if amp-bind-macro would support actions (or a new extension |
@sebastianbenz Good idea to use concat. That made me realize I could actually avoid manipulating the body class state entirely, in favor of dynamically concatenating a class name based on whether or not some other state value changes: <body <?php body_class(); ?> [class]="minnow.bodyClasses.concat( minnow.navMenuExpanded ? 'sidebar-open' : '' ).filter( className => '' != className )">
<amp-state id="minnow">
<?php
$state = array(
'bodyClasses' => get_body_class(),
'navMenuExpanded' => false,
)
?>
<script type="application/json"><?php echo wp_json_encode( $state ); ?></script>
</amp-state> Given this value of the Note: The Aside: I found that if you return an array as the value of |
What I wrote in #1032 (comment) was wrong. It is trivial to toggle state already using straight <button
class="menu-toggle"
aria-controls="primary-menu"
aria-expanded="false"
on="tap:AMP.setState( { siteNavigationMenu: { expanded: ! siteNavigationMenu.expanded } } )"
[aria-expanded]="siteNavigationMenu.expanded ? 'true' : 'false'"
>... |
:-) even better. I thought being able to toggle multiple classes on the same element was your problem, which can result in awkward nested if statements. |
I believe we discussed this before, but I think it might the time to do this.
or similar.
I'd like to avoid this being used to toggle ads, so I was thinking to limit it on elements that do not contain ads. One could still e.g. change zIndexes to put ads behind content. Anyone have an idea how to make it more foolproof?
CC @dvoytenko @rudygalfi
The text was updated successfully, but these errors were encountered: