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

TextField and Select do not suppport conditionals around named slots for leadingIcon and trailingIcon #230

Closed
morgandixon opened this issue Apr 26, 2021 · 2 comments
Assignees
Labels
enhancement New feature or request upstream Something is wrong with an upstream library, and it affects SMUI.

Comments

@morgandixon
Copy link
Contributor

morgandixon commented Apr 26, 2021

Describe the bug
TextField and Select components render icons incorrectly when they are wrapped in a conditional. This occurs in SMUI 3 and up.

To Reproduce

<TextField ...>
  {#if leadingIcon}
    <Icon slot="leadingIcon" ... />
  {/if}
...
</TextField>

Expected behavior
If there is a conditional wrapping a named slot, and the conditional evaluates to true, the component should render the same as if there was no conditional at all.

Screenshots
Here's an example of is what is output when leadingIcon is truthy:
image

I think this is because these components are using $$slots to provide class names but is not re-rendering when $$slots changes:

'mdc-text-field--with-leading-icon': $$slots.leadingIcon,
)

This looks like it's related to Svelte $$slots not updating with conditionals: sveltejs/svelte#5312

@morgandixon morgandixon added the bug Something isn't working label Apr 26, 2021
@hperrin hperrin self-assigned this Apr 26, 2021
@hperrin hperrin added the upstream Something is wrong with an upstream library, and it affects SMUI. label Apr 26, 2021
@hperrin
Copy link
Owner

hperrin commented Apr 26, 2021

This is a limitation in Svelte's slots and $$slots object. Slot values must be direct descendants of their components (they can't be in conditional blocks). Also, $$slots never changes after the initial render, so SMUI isn't updated about newly filled slots. There is a workaround however, for trailing icon. Since the style doesn't affect the label, you can wrap the conditional icon in a svelte:fragment that goes in the slot. Something like this:

<Textfield bind:value label="My Input">
    <svelte:fragment slot="trailingIcon">
      {#if !invalid}
        <Icon
          class="material-icons"
          role="button"
          on:click={clickHandler}>send</Icon
        >
      {/if}
    </svelte:fragment>
</Textfield>

But for leading icons, since the style affects the label, there's currently nothing you can do unless and until slots can be used conditionally: sveltejs/svelte#6218

I could add props for leadingIcon and trailingIcon that would override the $$slots check, then you could do that same svelte:fragment trick with leadingIcon. Would that be helpful?

@morgandixon
Copy link
Contributor Author

Adding props for leadingIcon and trailingIcon would be helpful. Then I wouldn't need to duplicate as much code for each conditional. Thanks!

@hperrin hperrin added enhancement New feature or request and removed bug Something isn't working labels Apr 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request upstream Something is wrong with an upstream library, and it affects SMUI.
Projects
None yet
Development

No branches or pull requests

2 participants