Cookie Consent by Free Privacy Policy Generator Update cookies preferences 📌 Customize your components: Class and Style Bindings 💅

🏠 Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeiträge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden Überblick über die wichtigsten Aspekte der IT-Sicherheit in einer sich ständig verändernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch übersetzen, erst Englisch auswählen dann wieder Deutsch!

Google Android Playstore Download Button für Team IT Security



📚 Customize your components: Class and Style Bindings 💅


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

Overview

In Vue it's possible to manipulate style of your components for assign dynamic value and create custom effects.
In this article will explain how to use this powerful feature.
Let's start! 🤙

Binding HTML Classes

For data binding it's necessary the v-model directive (if you don't remember or don't know, you can read this article).

There are two ways for use the style binding: Binding to Objects or Binding to Array.
Let's start with the first one.

Object

<script setup>
import { ref } from "vue"

const hasError = ref(false);
</script>

<template>
   <div :class="{'text-danger': hasError }"></div>
</template>

text-danger is a CSS class and will be rendered when hasError will be true.
Yes, hasError is a simply JavaScript boolean value, in this case wrapped by ref(), but it's possible use anything JavaScript expression, for example:

<script setup>
import { ref } from "vue"

const someProp = ref(0);
</script>

<div :class="{'someClass': someProp === 0 ? true : false }"></div>

Computed properties are welcome and it's possible use it, in this way:

<script setup>
import { ref, computed } from "vue"

const isActive = ref(true);
const error = ref(null);

const computedProp = computed(() => ({
  'someProp': isActive.value || !error.value,
  'text-danger': error.value && error.value.type === 'danger'
}))
</script>

<template>
   <div :class="computedProp"></div>
</template>

Array

We can bind :class to an array to apply a list of classes in this way:

<script setup>
import { ref } from "vue"

const activeClass = ref('active');
const errorClass = ref('text-danger');
</script>

<template>
   <div :class="[activeClass, errorClass]"></div>
</template>

This is the simplest example of using Arrays.
As for Object it's possible use anything JavaScript expression:

<div :class="[customClass ? isActive : '', anotherClass]"></div>

Binding Inline Styles

As for class, there are same ways for binding custom style.

Object

<script setup>
import { ref } from "vue"

const paragraphColor = ref('red');
const fontSize = ref(30);
</script>

<template>
   <p :style="{ color: activeColor, fontSize: fontSize + 'rem' }"></p>
</template>

Although camelCase keys are recommended, :style also supports kebab-cased CSS property keys,for example:

  <p :style="{ 'font-size': fontSize + 'rem' }"></p>

To keep clean code, it's a good idea declare an object for style the code in this way:

<script setup>
import { reactive } from "vue"

const paragraph = reactive({
  color: 'red',
  fontSize: '13px'
})
</script>

<template>
   <p :style="paragraph"></p>
</template>

Again, object style binding is often used in conjunction with computed properties that return objects.

Array

We can bind :style to an array of multiple style objects. These objects will be merged and applied to the same element:

<div :style="[baseStyles, overridingStyles]"></div>

Vendor Prefix

When you use a CSS property that requires a vendor prefix Vue will automatically add the appropriate prefix: it does this by checking at runtime to see which style properties are supported in the current browser. If the browser doesn't support a particular property then various prefixed variants will be tested to try to find one that is supported.

Conclusion

Vue provides style binding to make your components custom, giving you the ability to create and manipulate the app's style at runtime.
Feature not to be underestimated given its power.
Happy coding!✨

...



📌 Customize your components: Class and Style Bindings 💅


📈 70.2 Punkte

📌 Converting React Class Components to Functional Components: A Checklist and Example


📈 32.72 Punkte

📌 Embracing Modern React: Transitioning from Class Components to Functional Components


📈 31.18 Punkte

📌 Understanding Function Components vs Class Components in 100 seconds.


📈 31.18 Punkte

📌 Function Components vs Class Components in React – With Examples


📈 31.18 Punkte

📌 🔄 Class Components vs Functional Components: A Lifecycle Journey in React 🔄


📈 31.18 Punkte

📌 Embracing Modern React: Transitioning from Class Components to Functional Components


📈 31.18 Punkte

📌 Customize Material Components for your product (Google I/O '18)


📈 27.46 Punkte

📌 How to Change and Customize Font Size, Style on iOS 16


📈 26.43 Punkte

📌 How to use @counter-style rule to customize list item using CSS ?


📈 24.9 Punkte

📌 Do not use lxappearance, it will disable your key bindings (at least most of them)


📈 23.38 Punkte

📌 Next.js 14: Server Components and Client Components Explained


📈 23.13 Punkte

📌 Your Gmail, your way 😉 Learn how to customize the look of your inbox in a few easy steps #Shorts


📈 22.94 Punkte

📌 Sinister Styled-Components: CSS in JS with Style


📈 22.17 Punkte

📌 Bridge the gap with Bindings to native iOS and Android SDK's | MonkeyFest USA 2020


📈 21.78 Punkte

📌 Triggers and Bindings Explained [12 of 16] | Beginner's Series to: Serverless


📈 21.78 Punkte

📌 Technical Update: Deprecation of MySQL 5.1/5.5, MariaDB 5.5/10.0/10.1, and Percona 5.5/5.6 PHP bindings


📈 21.78 Punkte

📌 Medium CVE-2017-18604: Sitebuilder dynamic components project Sitebuilder dynamic components


📈 21.6 Punkte

📌 Typescript for React Components (or How To Write Components in React The Right Way)


📈 21.6 Punkte

📌 React Components 101: Building Reusable Components


📈 21.6 Punkte

📌 Lightning Web Components: Custom Nested Components


📈 21.6 Punkte

📌 Implementing Higher Order Components (HOC) for Functional Components in ReactJS


📈 21.6 Punkte

📌 React Controlled Components V/S Uncontrolled Components


📈 21.6 Punkte

📌 How to use React Server Components components on Storybook 8


📈 21.6 Punkte

📌 Clipboard 0.5.0 - Remove with regex, make your clipboards notable, customize your color scheme, and no more data races!


📈 21.34 Punkte

📌 Build and customize your own website with Wix, now 70% off your first year


📈 21.34 Punkte

📌 GitHub - astrelsky/Ghidra-Cpp-Class-Analyzer: Ghidra C++ Class and Run Time Type Information Analyzer


📈 20.71 Punkte

📌 The JavaScript Class Handbook – Complete Guide to Class Fields and the Super Keyword


📈 20.71 Punkte

📌 CVE-2022-2706 | SourceCodester Online Class and Exam Scheduling System 1.0 /pages/class_sched.php class sql injection


📈 20.71 Punkte

📌 What is a singleton class in Java? And How to implement a singleton class?


📈 20.71 Punkte

📌 How To Fix Undefined 'this.state' in React Class Components


📈 20.38 Punkte











matomo