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

Inline Styles #213

Closed
bnikkhah opened this issue Mar 1, 2020 · 7 comments
Closed

Inline Styles #213

bnikkhah opened this issue Mar 1, 2020 · 7 comments

Comments

@bnikkhah
Copy link

bnikkhah commented Mar 1, 2020

Is it possible to have inline styles in AlpineJS? I've tried something on CodePen, but nothing was working. Consider the following code:

<div class="box" :style="{ backgroundColor: 'red' }"></div>

Assuming .box has width: 100px;, height: 100px, and a background-color: 'blue', the code above should make the box the color of red, but it does not.

I've tried the same code in VueJS and it did work with inline styles. Here is the same code in VueJS:

<div id="app">
  <div class="box" :style="{ backgroundColor: 'red' }"></div>
</div>

Is there an extra step to consider when working with inline styles in AlpineJS? I didn't see anything mentioned about inline styles in the GitHub documentation.

Thanks for the help!

@tlgreg
Copy link
Contributor

tlgreg commented Mar 1, 2020

Object syntax in inline styles is not supported at the moment.
For the time being you can construct the string if you need dynamic inline styles.

<div x-data="{ color: 'red' }">
  <div :style="`background-color: ${color}`"></div>
</div>

@bnikkhah
Copy link
Author

bnikkhah commented Mar 1, 2020

Ah, I see. So it's not a feature yet. Okay, no problem!

By the way, this is quite an excellent framework. jQuery is good in terms of targeting elements without adding extra code to the DOM, but can get troubling with the query selectors (especially loops). VueJS is also good in terms of being a very robust framework, but might be too big for some cases. AlpineJS really delivers that mid-point front-end developers look for!

@calebporzio
Copy link
Collaborator

Thanks for the kind words @bnikkhah

Yeah, it's a feature that really should be added, I just haven't gotten to it.

@HugoDF
Copy link
Contributor

HugoDF commented Mar 5, 2020

@calebporzio is it cool if I give this a shot? Any weird cases you're aware of?

@HugoDF
Copy link
Contributor

HugoDF commented May 10, 2020

For anyone still looking/confused as to how to achieve this, I've found the stylenames package and forked it https://github.com/shatstack/stylenames

It's installable from npm with npm install @shat/stylenames or from CDN using <script src="https://cdn.jsdelivr.net/npm/@shat/[email protected]/lib/index.umd.js"></script>.

You can then use it as follows:

<div x-data="">
  <div class="box" :style="styleNames({ backgroundColor: 'red' })"></div>
</div>

@HugoDF
Copy link
Contributor

HugoDF commented May 11, 2020

I'll close this for now, the proposed changes aren't going into v2 and I'm supporting a package to achieve this kind of functionality outside of Alpine.js

Feel free to comment and reopen.

@HugoDF HugoDF closed this as completed May 11, 2020
@gemae
Copy link

gemae commented Sep 7, 2022

Hi, how about alpine dynamic inline style with conditional? someone knows here the syntax?

<div x-data="{active: true,color:'red'}">
  <div :style="`background-color: ${color}`"></div>
</div>

How do i insert the conditional . that it will just initialize once passed the condition

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants