QwikDev / qwik

Instant-loading web apps, without effort
https://qwik.dev
MIT License
20.46k stars 1.26k forks source link

[✨] Access Slots in parent component #4892

Closed GrandSchtroumpf closed 1 month ago

GrandSchtroumpf commented 11 months ago

Is your feature request related to a problem?

I've got a Select element and I want to display the content of the selected option :

<Select value="a">
  <Option value="a">Hello <b>World</b></Option>
  <Option value="b">Lorem Ipsum</Option>
</Select>

Here the Select has the default value selected to "a" so it should display the content of the Option with value "a". As we cannot access the DOM in the server, I wanted to access the elements in the Slot and find the one with the value. Unfortunately, this is possible with children, but not with Slot.

Describe the solution you'd like

Access a Readonly list of the JSXNode like you would do with children. Maybe something like that :

export const Select = component$(({ value, placeholder }: SelectProps) => {
  const slots = useSlot(); // snapshot of the elements in the Slot
  const initialContent = slots.default.find((option) => option.props.value === value) ?? '';
  const displayContent = useSignal(initialContent);

  return <>
    <button>{displayContent.value ?? placeholder}</button>
    <dialog>
      <Slot/> {/* Options here */}
    </dialog>
  </>
});

Describe alternatives you've considered

Use inline component and children. Or use DOM manipulation in the server if it becomes possible.

Additional context

No response

shairez commented 1 month ago

Thanks for the feature suggetion! closing for now because of: https://github.com/QwikDev/qwik/issues/3966#issuecomment-2131241498