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

Let's hack <style> #3

Open
egoist opened this issue Jan 8, 2019 · 7 comments
Open

Let's hack <style> #3

egoist opened this issue Jan 8, 2019 · 7 comments

Comments

@egoist
Copy link
Owner

egoist commented Jan 8, 2019

In:

<template>
  <h1>hello</h1>
</template>

<script>
export default {
  props: ['color', 'fontSize']
}
</script>

<style scoped>
h1 {
  color: eval("color");
  font-size: eval("fontSize * 2");
}
</style>

Out:

<template>
  <h1 :style="{'--v0': color, '--v1': fontSize * 2}">hello</h1>
</template>

<script>
export default {
  props: ['color', 'fontSize']
}
</script>

<style scoped>
h1 {
  color: var(--v0);
  font-size: var(--v1);
}
</style>
@atinux
Copy link

atinux commented Jan 8, 2019

I love it

@IniZio
Copy link

IniZio commented Jan 9, 2019

or maybe interpolation like

<style scoped>
h1 {
  color: {{ color }};
  font-size: {{ fontSize * 2 }};
}
</style>

?

Edit: Not friendly for editors since it is invalid css

@egoist
Copy link
Owner Author

egoist commented Jan 9, 2019

@IniZio I thought about that, but that's invalid CSS so editor may report errors.

@IniZio
Copy link

IniZio commented Jan 9, 2019

Guess eval is the way to go then 🤔

@danielwaltz
Copy link

danielwaltz commented Jan 14, 2019

I like this concept. I'm way more willing to adopt this method if I can continue using the style block in my SFCs.

Perhaps setting a new type will help make it more obvious to fresh eyes on the project what's going on too. Like <style lang="styled" scoped> or something like that.

@cainrus
Copy link

cainrus commented Jan 17, 2019

What do you think about auto-injection of css variables?

In:

<template>
  <h1>hello</h1>
</template>

<script>
export default {
  props: ['color', 'size'],
  data: () => ({ bgColor: 'red' }),
  computed: { fontSize() { return this.size + 'px';  } },
}
</script>

<style scoped>
h1 {
  color: var(--vm-color);
  font-size: var(--vm-fontSize);
  background-color: var(--vm-bgColor);
}
</style>

Out:

<template>
  <h1 :style="{'--vm-color': color, '--vm-fontSize': fontSize, '--vm-bgColor': bgColor}">hello</h1>
</template>

<script>
export default {
  props: ['color', 'size'],
  data: () => ({ bgColor: 'red' }),
  computed: { fontSize() { return this.size + 'px';  } },
}
</script>

<style scoped>
h1 {
  color: var(--vm-color);
  font-size: var(--vm-fontSize);
  background-color: var(--vm-bgColor);
}
</style>

@egoist
Copy link
Owner Author

egoist commented Jan 18, 2019

@cainrus maybe something like this instead: #6

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

No branches or pull requests

5 participants