|
| 1 | +# defineStyleX <PackageVersion name="@vue-macros/define-stylex" /> |
| 2 | + |
| 3 | +<StabilityLevel level="experimental" /> |
| 4 | + |
| 5 | +Define and consume [StyleX](https://stylexjs.com/) styles in `<script setup>`. |
| 6 | + |
| 7 | +| Features | Supported | |
| 8 | +| :----------------: | :----------------: | |
| 9 | +| Vue 3 | :white_check_mark: | |
| 10 | +| Nuxt 3 | :white_check_mark: | |
| 11 | +| TypeScript / Volar | :white_check_mark: | |
| 12 | +| Vue 2 | :white_check_mark: | |
| 13 | + |
| 14 | +## Setup |
| 15 | + |
| 16 | +To use StyleX, you should install and configure StyleX first. The steps could change, you may want to check the [official documentation](https://stylexjs.com/) and the [documentation of vite-plugin-stylex](https://github.com/HorusGoul/vite-plugin-stylex) for the latest information. |
| 17 | + |
| 18 | +### Vite |
| 19 | + |
| 20 | +```sh |
| 21 | +pnpm add @stylexjs/stylex vite-plugin-stylex |
| 22 | +``` |
| 23 | + |
| 24 | +```ts [vite.config.ts] {4,13} |
| 25 | +import Vue from '@vitejs/plugin-vue' |
| 26 | +import VueMacros from 'unplugin-vue-macros/vite' |
| 27 | +import { defineConfig } from 'vite' |
| 28 | +import StyleX from 'vite-plugin-stylex' |
| 29 | + |
| 30 | +export default defineConfig({ |
| 31 | + plugins: [ |
| 32 | + VueMacros({ |
| 33 | + plugins: { |
| 34 | + vue: Vue(), |
| 35 | + }, |
| 36 | + }), |
| 37 | + StyleX(), // Must be placed after Vue Macros |
| 38 | + ], |
| 39 | +}) |
| 40 | +``` |
| 41 | + |
| 42 | +```vue [App.vue] {2-3} |
| 43 | +<style> |
| 44 | +/* import StyleX stylesheet, according to https://github.com/HorusGoul/vite-plugin-stylex */ |
| 45 | +@stylex stylesheet; |
| 46 | +</style> |
| 47 | +``` |
| 48 | + |
| 49 | +## Basic Usage |
| 50 | + |
| 51 | +```vue [App.vue] twoslash |
| 52 | +<script setup lang="ts"> |
| 53 | +const styles = defineStyleX({ |
| 54 | + red: { color: 'red' }, |
| 55 | +}) |
| 56 | +
|
| 57 | +// ... |
| 58 | +// ---cut-start--- |
| 59 | +declare const vStylex: any |
| 60 | +// ---cut-end--- |
| 61 | +</script> |
| 62 | +
|
| 63 | +<template> |
| 64 | + <p v-stylex="styles.red">Red</p> |
| 65 | +</template> |
| 66 | +``` |
| 67 | + |
| 68 | +:::details Compiled Code (with some simplifications) |
| 69 | + |
| 70 | +```vue [App.vue] twoslash |
| 71 | +<script lang="ts"> |
| 72 | +const styles = _stylex_create({ |
| 73 | + red: { color: 'red' }, |
| 74 | +}) |
| 75 | +</script> |
| 76 | +
|
| 77 | +<script setup lang="ts"> |
| 78 | +import { |
| 79 | + attrs as _stylex_attrs, |
| 80 | + create as _stylex_create, |
| 81 | +} from '@stylexjs/stylex' |
| 82 | +
|
| 83 | +// ... |
| 84 | +</script> |
| 85 | +
|
| 86 | +<template> |
| 87 | + <p v-bind="_stylex_attrs(styles.red)">Red</p> |
| 88 | +</template> |
| 89 | +``` |
| 90 | + |
| 91 | +::: |
| 92 | + |
| 93 | +## Conditional Styles |
| 94 | + |
| 95 | +Optional and multiple rules are supported. |
| 96 | + |
| 97 | +```vue [App.vue] twoslash |
| 98 | +<script setup lang="ts"> |
| 99 | +defineProps<{ bold?: boolean }>() |
| 100 | +
|
| 101 | +const styles = defineStyleX({ |
| 102 | + red: { color: 'red' }, |
| 103 | + bold: { fontWeight: 'bold' }, |
| 104 | +}) |
| 105 | +// ---cut-start--- |
| 106 | +declare const vStylex: any |
| 107 | +// ---cut-end--- |
| 108 | +</script> |
| 109 | +
|
| 110 | +<template> |
| 111 | + <span v-stylex="(styles.red, bold && styles.bold)">Red</span> |
| 112 | +</template> |
| 113 | +``` |
| 114 | + |
| 115 | +:::details Compiled Code (with some simplifications) |
| 116 | + |
| 117 | +```vue [App.vue] twoslash |
| 118 | +<script lang="ts"> |
| 119 | +const styles = _stylex_create({ |
| 120 | + red: { color: 'red' }, |
| 121 | + bold: { fontWeight: 'bold' }, |
| 122 | +}) |
| 123 | +</script> |
| 124 | +
|
| 125 | +<script setup lang="ts"> |
| 126 | +import { |
| 127 | + attrs as _stylex_attrs, |
| 128 | + create as _stylex_create, |
| 129 | +} from '@stylexjs/stylex' |
| 130 | +
|
| 131 | +defineProps<{ bold?: boolean }>() |
| 132 | +</script> |
| 133 | +
|
| 134 | +<template> |
| 135 | + <span v-bind="_stylex_attrs(styles.red, bold && styles.bold)">Red</span> |
| 136 | +</template> |
| 137 | +``` |
| 138 | + |
| 139 | +::: |
0 commit comments