Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Svelte: allow popovers to have external targets#63141

Merged
camdencheek merged 8 commits into
mainfrom
cc/external-popover-target
Jun 7, 2024
Merged

Svelte: allow popovers to have external targets#63141
camdencheek merged 8 commits into
mainfrom
cc/external-popover-target

Conversation

@camdencheek

@camdencheek camdencheek commented Jun 6, 2024

Copy link
Copy Markdown
Member

This updates the Popover component to accept a passed-in target rather than setting the target via an action. This allows us to pass in targets not owned by the Popover component, which is important for things like the file tree where the logical target is a wrapper around what the consumer has access to.

Test plan

Manually tested. Video walkthrough.

@cla-bot cla-bot Bot added the cla-signed label Jun 6, 2024
Comment on lines +18 to +19
export let trigger: HTMLElement | null = null
export let target: HTMLElement | undefined = undefined

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Primary new change: expose trigger and target so they can be passed in directly rather than only via the registerTrigger/registerTarget actions.

Using a link here allows us to benefit from data preloading.
-->
<Popover let:registerTrigger placement="right-end" showOnHover>
<Popover trigger={label} placement="right-start" showOnHover offset={{ mainAxis: 8, crossAxis: -32 }}>

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how the passed-in trigger looks. (the change in placement and offset is not related, but necessary to make this look okay)

@camdencheek camdencheek marked this pull request as ready for review June 6, 2024 22:33
@camdencheek camdencheek requested a review from a team June 6, 2024 22:33
Comment on lines +104 to 111
// Every time trigger changes (either by a props change or a call to registerTrigger)
// unwatch the old trigger and watch the new trigger.
let oldTrigger: HTMLElement | null
$: {
oldTrigger && unwatchTrigger(oldTrigger)
trigger && watchTrigger(trigger)
oldTrigger = trigger
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope we can eventually provide a nice API for this use case, which is basically an effect with cleanup logic. Maybe something like

const trackedTrigger = track(trigger => {
    if (trigger) {
        watchTrigger(trigger)
        return () => unwatchTrigger(trigger)
    }
})
$: trackedTrigger.set(trigger)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed! I was honestly kinda surprised that there wasn't something built in to svelte to get the previous value of the variable.

@camdencheek camdencheek merged commit a36b750 into main Jun 7, 2024
@camdencheek camdencheek deleted the cc/external-popover-target branch June 7, 2024 15:28
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants