feat : support transition with hidden (poc)
A possible solution for #9976
It's simple to show/hide a node with the hidden attribute (of style:display), but there no way to use the svelte's transition with that, and the {#if} block destroys its contents every time the condition changes, which is not necessarily desirable.
This PR proposes a new modifier for the transition: directive : hidden.
With this modifier, the transition will only be applied when the hidden attribute is modified.
Example :
<div transition:slide|hidden={options} hidden={condition}>
</div>
This would allow to show/hide elements using Svelte transitions.
Demo REPL here (edit : modified REPL to use |this instead of |hidden)
Remarks :
- I don't really like the modifier's name
hidden, but I couldn't find a better name... - The implementation is quite simple.
- I just realized that this doesn't work with spreading, but I think it should be relatively easy to implement.
- Compatible SSR without change, as the hidden attribute will be set.
- Caveat : the
hiddenattribute is overridden by any CSSdisplayvalue. It's required to have a global CSS like that :
[hidden] {
display: none !important;
}
Is it worth it?
Svelte 5 rewrite
Please note that the Svelte codebase is currently being rewritten for Svelte 5. Changes should target Svelte 5, which lives on the default branch (main).
If your PR concerns Svelte 4 (including updates to svelte.dev.docs), please ensure the base branch is svelte-4 and not main.
Before submitting the PR, please make sure you do the following
- [X] It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
- [X] Prefix your PR title with
feat:,fix:,chore:, ordocs:. - [X] This message body should clearly illustrate what problems it solves.
- [ ] Ideally, include a test that fails without this PR but passes with it.
Tests and linting
- [ ] Run the tests with
pnpm testand lint the project withpnpm lint
⚠️ No Changeset found
Latest commit: f5f6317600204fed80298524081fc54fca30e02b
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
This PR includes no changesets
When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR
For the naming, perhaps we could use different directive, for example by replacing transition:/in:/out: with someting like view:/show:/hide:
-<div transition:slide|hidden={options} hidden={condition}></div>
+<div view:slide={options} hidden={condition}></div>
-<div in:slide|hidden={options} out:fade|hidden={options} hidden={condition}></div>
+<div show:slide={options} hide:fade={options} hidden={condition}></div>
I fixed some bug with in: & out: directive, and support for spreading with set_attributes().
I also changed the modifier from |hidden to |this which I think is a little more explicit...
Just see https://github.com/sveltejs/svelte/pull/13612 I didn't know that hidden could take other values, which conflicts a bit with this PR... :/
closed as issue is closed and hidden may not be the best choice for that