Skip to content
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

fix(ui): popover toggle fix #280

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions codex-ui/dev/pages/components/Popover.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<div>
<Button
secondary
@click="show($event.target, {vertically: 'below', horizontally: 'left'})"
@click="!isOpen ? show($event.target, {vertically: 'below', horizontally: 'left'}) : hide()"
>
Open below left
</Button>
Expand Down Expand Up @@ -82,7 +82,7 @@
import PageHeader from '../../components/PageHeader.vue';
import { usePopover, PopoverShowParams, Button, ContextMenu, Heading } from '../../../src/vue';

const { showPopover } = usePopover();
const { showPopover, isOpen, hide } = usePopover();

/**
* Example of working with Popover
Expand Down
8 changes: 7 additions & 1 deletion codex-ui/src/vue/components/popover/Popover.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@ const {
hide,
content,
width,
targetElement,
} = usePopover();

/**
* Close the popover when clicking outside of it
*/
onClickOutside(popoverEl, hide);
onClickOutside(popoverEl, hide, {
/**
* Allow clicks on the target element to implemet toggle behavior
*/
ignore: [targetElement],
});
</script>

<style module>
Expand Down
8 changes: 8 additions & 0 deletions codex-ui/src/vue/components/popover/usePopover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ export const usePopover = createSharedComposable(() => {
*/
const content = shallowRef<PopoverContent | null>(null);

/**
* Target element to move popover to
*/
const targetElement = ref<HTMLElement | null>(null);

/**
* Move popover to the target element
* Also, align and set width
Expand Down Expand Up @@ -128,6 +133,7 @@ export const usePopover = createSharedComposable(() => {
* @param params - popover showing configuration
*/
function showPopover(params: PopoverShowParams): void {
targetElement.value = params.targetEl;
move(params.targetEl, params.align, params.width);
mountComponent(params.with.component, params.with.props);
show();
Expand All @@ -137,6 +143,7 @@ export const usePopover = createSharedComposable(() => {
* Empty content, position and hide popover
*/
function resetPopover(): void {
targetElement.value = null;
content.value = null;
position.left = 0;
position.top = 0;
Expand All @@ -159,5 +166,6 @@ export const usePopover = createSharedComposable(() => {
hide,
content,
width,
targetElement,
};
});
Loading