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

Hide PopoverPanel on href/link click #26

Open
DaliborTrampota opened this issue Jan 3, 2023 · 1 comment
Open

Hide PopoverPanel on href/link click #26

DaliborTrampota opened this issue Jan 3, 2023 · 1 comment

Comments

@DaliborTrampota
Copy link

DaliborTrampota commented Jan 3, 2023

Hi,

I have this component

function ProfileButton() {
    const { logged, user, login, logout } = useAuth()

    return <Show
            when={logged() && user()}
            fallback={<LoginIcon class="h-10 w-10 rounded-md cursor-pointer hover:bg-white hover:bg-opacity-25 p-1" onClick={login}/>}
        >
            <Popover class="relative text-dark" defaultOpen={false}>
                {({ isOpen }) => (
                    <>
                        <div>
                            <PopoverButton class="menu-btn">
                                <ChevronUp class={`${!isOpen() && 'rotate-180'} w-6 h-6 text-dark self-center ml-1 sm:block hidden transition ease-out duration-100`}/>
                                <p class='mx-2 text-lg font-medium sm:block hidden'>{user().username}</p>
                                <img class="sm:h-10 sm:w-10 h-12 w-12 rounded-full" src={`https://cdn.discordapp.com/avatars/${user().id}/${user().avatar}.png?size=512`} alt="profile"/>
                            </PopoverButton>
                        </div>
                        <Transition
                            show={isOpen()}
                            enter="transition ease-out duration-100"
                            enterFrom="transform opacity-0 scale-95"
                            enterTo="transform opacity-100 scale-100"
                            leave="transition ease-in duration-75"
                            leaveFrom="transform opacity-100 scale-100"
                            leaveTo="transform opacity-0 scale-95"
                        >
                            <PopoverPanel class="nav-menu">
                                <Menu>
                                    <MenuItem>
                                        <A href="/profile" class="nav-menu-item">Your Profile</A>
                                        {/* <A href="/profile/connections" class="nav-menu-item">Connections</A> */}
                                        <Show when={user().admin}>
                                            <A href="/admin/premium" class="nav-menu-item">Admin Panel</A>
                                        </Show>
                                    </MenuItem>
                                    <MenuItem>
                                        <div onClick={logout} id="logout-btn">Sign Out</div>
                                    </MenuItem>
                                </Menu>
                            </PopoverPanel>
                        </Transition>
                    </>
                )}
            </Popover>
        </Show>
}

I want the to hide/close the popover panel when an component is clicked (from solid-router redirect/navigate) how can I achieve that?

also on the vercel page are only 4 components shown? is that correct? or is it wrong link?
and lastly, is there a discord server for this package?

thanks

@pllffrd
Copy link

pllffrd commented Feb 17, 2024

you can use the close() function provided by the Popover component's props. This function allows you to programmatically close the popover menu when a user clicks on an item.

Here's an updated version of your component:

function ProfileButton() {
    const { logged, user, login, logout } = useAuth()

    return <Show
            when={logged() && user()}
            fallback={<LoginIcon class="h-10 w-10 rounded-md cursor-pointer hover:bg-white hover:bg-opacity-25 p-1" onClick={login}/>}
        >
            <Popover class="relative text-dark" defaultOpen={false}>
                
                {({ isOpen, close }) => (
                    <>
                        <div>
                            <PopoverButton class="menu-btn">
                                <ChevronUp class={`${!isOpen() && 'rotate-180'} w-6 h-6 text-dark self-center ml-1 sm:block hidden transition ease-out duration-100`}/>
                                <p class='mx-2 text-lg font-medium sm:block hidden'>{user().username}</p>
                                <img class="sm:h-10 sm:w-10 h-12 w-12 rounded-full" src={`https://cdn.discordapp.com/avatars/${user().id}/${user().avatar}.png?size=512`} alt="profile"/>
                            </PopoverButton>
                        </div>
                        <Transition
                            show={isOpen()}
                            enter="transition ease-out duration-100"
                            enterFrom="transform opacity-0 scale-95"
                            enterTo="transform opacity-100 scale-100"
                            leave="transition ease-in duration-75"
                            leaveFrom="transform opacity-100 scale-100"
                            leaveTo="transform opacity-0 scale-95"
                        >
                            <PopoverPanel class="nav-menu">
                                <Menu>
                                    <MenuItem>
                                       
                                        <A href="/profile" onClick={() => close()} class="nav-menu-item">Your Profile</A>
                                        {/* <A href="/profile/connections" class="nav-menu-item">Connections</A> */}
                                        <Show when={user().admin}>
                                            <A href="/admin/premium" onClick={() => close()} class="nav-menu-item">Admin Panel</A>
                                        </Show>
                                    </MenuItem>
                                    <MenuItem>
                                        <div onClick={logout} id="logout-btn">Sign Out</div>
                                    </MenuItem>
                                </Menu>
                            </PopoverPanel>
                        </Transition>
                    </>
                )}
            </Popover>
        </Show>
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants