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
Usage of x-transition with responsive elements #235
Comments
I understand that you have an element that will only show on small screens, and assuming you’re using Tailwindcss, we could hack something like this: <div
x-show=“open”
class=“sm:hidden”
>
...
</div> Now, let me explain:
With this, you should be able to keep adding transitions and animations on the small screen, while keeping everything hidden for the bigger ones. |
Yes this could work, provided that the class styles are more important than the elements inline styles that Alpine applies. This is presently (not always) the case, right? |
Class is prioritized over Inline-styling in all browsers, so this should work to keep the “toggle” only work for the small screen |
Ok well in that case, this solves my issue. Thanks a million! |
Glad to help! Try it out and ping if this solves your issues or if it doesn’t 😄 |
Are you sure about this? Pretty sure you can override class contents with an inline style. |
@HugoDF, you’re right, my bad, inline style property has higher priority. Thanks for the heads up! Anyway, for this specific case it will not overlap the logic given before.
|
😉 I guess your fix didn't rely on that precedence rule 👍 |
I've re-opened this issue since it is something that might need a better workflow. It indeed works in this particular case because the styles are defined mobile-first and we anyway hide the menu on larger screens. If I wanted to do the opposite (hide menu on small screens, toggle on larger) it would not work. |
I think this is becoming more of a CSS thing than an Alpinejs one... But let’s give it a shot... For the inverse, hide on small screen and show on bigger ones, do something like this: <div
x-show=“open”
class=“hidden sm:block”
>
...
</div> Note: The class |
Inline styles will always override CSS classes unless the CSS class has The way around this would be either create classes with |
@FYITom I don’t think I’m understanding your whole point, but as far as I can understand, you’re defining display classes during transition steps, which I think you should avoid. Instead, define display classes in the object class directly, and keep animations or transforms at transition steps. The following is present in one of my projects. It properly switches <div
x-show=“open”
x-transition:enter=“transform duration-200”
x-transition:enter-start=“opacity-0 scale-90”
x-transition:enter-end=“opacity-100 scale-100”
x-transition:leave=“transform duration-200”
x-transition:leave-start=“opacity-100 scale-100”
x-transition:leave-end=“opacity-0 scale-90”
class=“block md:hidden p-4 border rounded shadow”
>
Hello there!
</div> |
@georgeboot Are you happy to close this issue? The responses above are correct and it's more about how css work then Alpine. To recap
Thanks |
Hmm I’m not fully sure. @FYITom points out some issues. I guess there are ways to work around this, but it isn’t ideal. Many people use Alpine together with Tailwind CSS, and this is a widely spread use case. For example: Tailwind UI (commercial product from guys of Tailwind CSS) has places where this is the exact thing. This means that no transitions are using on mobile or desktop only elements, simple because it’s a lot of hacking to get it to work. On the other hand don’t I like to keep an issue open, while I’m myself not actively trying about ways to fix it. So there’s that. I think if we could come up with something like Worth exploring? |
@georgeboot isn't that already possible with either the |
So, what you’re looking for is for screen resize class transitions @georgeboot? Or merely class switching based on screen sizing? |
@HugoDF yes you can toggle classes using :class. But if you want to use x-transition, you need to use x-if or x-show. |
I think not everyone is getting my point here. Let me summarise. If you want to use Inline styles take precedence over classes. Because if this, some responsive scenarios are not possible. For example: you want to toggle and animate an element on mobile screens, but have it always visible on larger screens. If you use If we use x-show, it would also hide in larger screens, even if we set |
@georgeboot Can you post a codepen or an example where we can replicate the issue so we can have a better understanding of the problem? Thanks |
Sorry, I posted while you were sending your second message. So you are setting a generic hidden and overriding large screens, right? Don't you get the same result if you hide small and medium screens? |
@georgeboot To clarify, I mean something like: UPDATE: It actually works with the generic class as well: https://codepen.io/SimoTod/pen/OJyqOjo Sorry, I think I need to see an example where it's not working to understand the issue because it looks like it should work to me. |
How about this for an example where it is not working. I'd like for the options to ALWAYS be displayed on large screens. However, on smaller screens, I'd like for it to be displayed only if it is toggled. https://codepen.io/nlehman06/pen/LYGoENq I know I can do it with :class toggling, but that will make it so I can't do transitions. |
What do you suggest @nlehman06? There are a few ways to do it. For example you could have a property set to true if the screen is large and update the property on |
Sorry, I don't really have much of a suggestion. I just ran into this issue myself and found this discussion and the last thing said was "I need to see an example" so I thought I'd post my current situation. Thanks for the work-arounds. I'll give the |
No worries, I asked in case you had something in mind. Alpine Transitions can be applied only on x-if and x-show. Applying them to x-bind:class would be quite challenging and would have poor performances: the code would have to understand if a class changed the status of the display property and call the transition in case (which means it should apply the class, check display, remove the class, start the transition and apply the class again, hoping the DOM won't flicker in the meantime). |
The above is (often) a little heavy just to solve this particular problem. I had a head-scratching moment (or to be honest: 2 head-scratching hours) with this particular problem myself. What I found is working nicely is something like this: @screen md {
.block-important {
display: block !important;
}
} <div class="block-important" x-show.transition="open">
</div> |
I think this issue can be closed now. As @SimoTod mentioned, supporting @bep has left a good tip above, but there is also the tip from @SimoTod about adding |
@ryangjchandler yeah agreed. Thanks for the solutions provided! |
@georgeboot - no worries, that's what we're here for! |
Hi, I have the same problem. I'll show the code:
If I delete What is the problem? Thank you so much! |
|
@SimoTod wow! Thank you so much!! I didn't know that I could use Thanks again! |
No worries. FYI, You can use any valid js statements inside Alpine directives. 👍 |
In case someone else comes across this, I made a Pen using @bep's solution: |
In tailwind 3 you can use sm:!block to add important to the styling. |
x-transition
only works withx-show
andx-if
.However, in many cases, these tools can't be used. For example, with an element that you only want to show on small screens.
In these cases, you wil probably use:
How could one still get transition support to these cases?
The text was updated successfully, but these errors were encountered: