### Svelte App Setup
Source: https://beta.cva.style/examples/svelte
Basic Svelte app setup including CSS import and mounting the root App component.
```typescript
import "./app.css";
import App from "./App.svelte";
import { mount } from "svelte";
const app = mount(App, {
target: document.getElementById("app")!,
});
export default app;
```
--------------------------------
### Install CVA with npm
Source: https://beta.cva.style/getting-started/installation
Use this command to install the beta version of CVA using npm.
```bash
npm i cva@beta
```
--------------------------------
### Install CVA with pnpm
Source: https://beta.cva.style/getting-started/installation
Use this command to install the beta version of CVA using pnpm.
```bash
pnpm i cva@beta
```
--------------------------------
### Install CVA with bun
Source: https://beta.cva.style/getting-started/installation
Use this command to add the beta version of CVA using bun.
```bash
bun add cva@beta
```
--------------------------------
### Install Tailwind CSS
Source: https://beta.cva.style/examples/react/tailwindcss
Install Tailwind CSS and its peer dependencies using npm or yarn. This is the initial setup step for using Tailwind CSS in your project.
```bash
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
```
--------------------------------
### Install CVA with yarn
Source: https://beta.cva.style/getting-started/installation
Use this command to add the beta version of CVA using yarn.
```bash
yarn add cva@beta
```
--------------------------------
### CSS Module File Example
Source: https://beta.cva.style/examples/react/css-modules
Example of a CSS Module file (`.module.css`) defining scoped styles. These class names will be automatically transformed into unique identifiers during the build process.
```css
.container {
padding: 20px;
border: 1px solid #ccc;
background-color: #f9f9f9;
}
.title {
color: navy;
font-size: 24px;
}
.text {
color: #333;
line-height: 1.6;
}
```
--------------------------------
### CSS Module File Example
Source: https://beta.cva.style/examples/react/css-modules
Example of a CSS Module file (`.module.css`) defining local class names. These class names are automatically made unique to prevent global scope pollution.
```css
.container {
padding: 20px;
border: 1px solid #ccc;
background-color: #f9f9f9;
}
.title {
color: navy;
font-size: 24px;
}
.text {
color: #333;
line-height: 1.5;
}
```
--------------------------------
### Svelte App.svelte Structure
Source: https://beta.cva.style/examples/svelte
Example structure for an App.svelte file demonstrating loops and conditional rendering.
```svelte
{#each intents as intent}
{#each isDisabled as disabled}
{#each sizes as size, index}
{#if index === 0}
{#each intents as intent}
{/each}
{/if}
{/each}
{/each}
{/each}
State
Size
Intent
{#each intents as intent}
{#each isDisabled as disabled}
{#each sizes as size, index}
{disabled ? "disabled" : "enabled"}
{size || "default"}
{intent || "default"}
{/each}
{/each}
{/each}
```
--------------------------------
### Responsive Variants Example with Tailwind CSS
Source: https://beta.cva.style/faqs
Demonstrates how to achieve responsive variants by conditionally rendering elements based on screen size using Tailwind CSS and CVA.
```jsx
export const Example = () => (
<>
>
);
```
--------------------------------
### CSS Module File with Composed Styles
Source: https://beta.cva.style/examples/react/css-modules
Example CSS Module file demonstrating style composition. The `.primary` class can be combined with `.button` to create a primary button style.
```css
.button {
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
}
.primary {
background-color: blue;
color: white;
}
```
--------------------------------
### Basic CSS Module Example
Source: https://beta.cva.style/examples/react/css-modules
Define styles in a CSS Module file. These styles are locally scoped to the component.
```css
.button {
background-color: blue;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
}
.button:hover {
background-color: darkblue;
}
```
--------------------------------
### Example Usage with tailwind-merge
Source: https://beta.cva.style/getting-started/installation
Demonstrates how `tailwind-merge` resolves class conflicts, ensuring that variant styles take precedence over base styles.
```typescript
import { cx, cva } from "../cva.config";
export const button = cva({
// 1. `twMerge` strips out `bg-gray-200`…
base: "font-semibold bg-gray-200 border rounded",
variants: {
intent: {
// 2. …as variant `bg-*` values take precedence
primary: "bg-blue-500 text-white border-transparent hover:bg-blue-600",
secondary: "bg-white text-gray-800 border-gray-400 hover:bg-gray-100",
},
},
defaultVariants: {
intent: "primary",
},
});
button();
// => "font-semibold border rounded bg-blue-500 text-white border-transparent hover:bg-blue-600 text-base py-2 px-4 uppercase"
cx("bg-gray-200", "bg-blue-500");
// => "bg-blue-500"
```
--------------------------------
### React Compound Component Example
Source: https://beta.cva.style/getting-started/compound-components
Demonstrates the structure of a compound component in React using CVA. This pattern is useful for building complex UI elements like accordions.
```jsx
import * as Accordion from "./Accordion";
function Example() {
return (
Section 1Content 1
);
}
```
--------------------------------
### Using the React Button Component with a Render Prop
Source: https://beta.cva.style/getting-started/polymorphism
Example of how to use the custom React Button component, passing a render prop to customize the rendered element. Shows how the component applies styles.
```javascript
import { Button } from "./components/button";
// Renders:
//
// Sign up
//
export default () => }>Contact;
```
--------------------------------
### Vite Configuration for React and Tailwind CSS
Source: https://beta.cva.style/examples/react/tailwindcss
This configuration file sets up Vite to use the React plugin and the Tailwind CSS plugin. Ensure these plugins are installed in your project.
```typescript
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import tailwindcss from "@tailwindcss/vite";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react(), tailwindcss()],
});
```
--------------------------------
### Button CSS Module File Example
Source: https://beta.cva.style/examples/react/css-modules
Define base button styles and conditional primary button styles in a CSS Module. The `composes` keyword can also be used for style inheritance.
```css
.button {
padding: 10px 15px;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
.primary {
background-color: blue;
color: white;
}
.secondary {
background-color: grey;
color: black;
}
```
--------------------------------
### Example React Component with Tailwind CSS
Source: https://beta.cva.style/examples/react/tailwindcss
A basic React component demonstrating the use of Tailwind CSS utility classes for styling. Apply classes directly to your JSX elements.
```jsx
function App() {
return (
Welcome to Tailwind CSS with React!
This is a styled paragraph using Tailwind utility classes.
)
}
export default App
```
--------------------------------
### Vue Button Component with Variants
Source: https://beta.cva.style/examples/vue
Defines a Vue.js Button component using the `