Skip to content

Instantly share code, notes, and snippets.

@edisdev
Last active April 11, 2023 13:34
Show Gist options
  • Save edisdev/c53d0959db41f94e40e84ee3ae42a928 to your computer and use it in GitHub Desktop.
Save edisdev/c53d0959db41f94e40e84ee3ae42a928 to your computer and use it in GitHub Desktop.
Vue 3 Example With Composition API and Custom CSS Variable
<template>
<div class="Example">
<h1>JS IN CSS Reactive Example</h1>
<div class="area">
<div class="form">
<label>Select Color</label>
<input type="color" v-model="customTheme.bgColor" />
</div>
<div class="preview">
<span>{{ customTheme.bgColor }}</span>
<div class="customColor"></div>
</div>
</div>
</div>
</template>
<script>
import { ref } from "vue";
export default {
name: "JSinCSS",
setup() {
const customTheme = ref({
bgColor: "",
});
return {
customTheme
};
},
};
</script>
<style>
.Example {
--custom-color: v-bind("customTheme.bgColor");
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
padding: 3rem;
}
.Example h1 {
margin-bottom: 20px;
}
.Example .area {
display: flex;
align-items: baseline;
}
.Example .area .form {
margin-right: 30px;
}
.Example .area .form label {
display: block;
}
.Example .area .form input {
width: 100px;
height: 50px;
}
.Example .area .preview {
font-weight: bold;
}
.customColor {
background: var(--custom-color);
width: 100px;
height: 50px;
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment