styled-vue icon indicating copy to clipboard operation
styled-vue copied to clipboard

Let's hack `<style>`

Open egoist opened this issue 6 years ago • 7 comments

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>

egoist avatar Jan 08 '19 16:01 egoist

I love it

atinux avatar Jan 08 '19 23:01 atinux

or maybe interpolation like

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

?

Edit: Not friendly for editors since it is invalid css

IniZio avatar Jan 09 '19 01:01 IniZio

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

egoist avatar Jan 09 '19 03:01 egoist

Guess eval is the way to go then :thinking:

IniZio avatar Jan 09 '19 04:01 IniZio

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.

danielwaltz avatar Jan 14 '19 20:01 danielwaltz

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>

cainrus avatar Jan 17 '19 21:01 cainrus

@cainrus maybe something like this instead: https://github.com/egoist/styled-vue/issues/6

egoist avatar Jan 18 '19 07:01 egoist