Hey, does anybody know how to change a state/context if one clicked off an element? #64466
Closed
Newton-Adams
started this conversation in
Interactivity API
Replies: 1 comment 2 replies
-
Nevermind... I found this works // render.php
<div data-wp-on-document--click="callbacks.logClick"></div> // view.js
store('services-dropdown', {
actions: {
toggle: () => {
const context = getContext();
context.isOpen = !context.isOpen;
},
},
callbacks: {
logIsOpen: () => {
const { isOpen } = getContext();
},
serviceList: () => {
const context = getContext();
const isOpen = context.isOpen; // Ensure `isOpen` is retrieved correctly
const isEleHeight = useEleHeight();
if (isOpen) {
context.dropDownHeight = `${isEleHeight}px`; // Ensure height is set as a string with 'px'
} else {
context.dropDownHeight = '0px';
}
},
logClick(event) {
if (!event.target.closest('.wp-block-create-block-services-dropdown')) {
const context = getContext();
context.isOpen = false;
}
},
},
}); |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a dropdown block pulling services from a custom post type. Open and closing the dropdown works fine when I click on the element. But I want to change the state when I click off of it or anywhere else off the element on the screen.
Please and thanks in advance
Beta Was this translation helpful? Give feedback.
All reactions