# SASS Usage Examples
This document provides practical examples of how to use the SASS system in MetaBuilder.
## Table of Contents
1. [Using Pre-built Classes](#using-pre-built-classes)
2. [Using Variables & Mixins](#using-variables--mixins)
3. [Creating Custom Components](#creating-custom-components)
4. [Responsive Design](#responsive-design)
5. [Theme Switching](#theme-switching)
6. [Advanced Techniques](#advanced-techniques)
---
## Using Pre-built Classes
### Buttons
```html
```
### Cards
```html
Content with shadow
```
### Forms
```html
```
### Badges
```html
New
Featured
Active
Pending
Offline
Info
```
### Alerts
```html
Heads up!
This is an informational message.
Success! Your changes have been saved.
Warning: Please review your input.
Error! Something went wrong. Please try again.
```
### Spacing Utilities
```html
Margin top 1rem, margin bottom 2rem
Padding 1.5rem all sides
Padding 1rem (mobile), 1.5rem (tablet), 2rem (desktop)
Centered horizontally
```
### Text Utilities
```html
Primary color text
Success message
Error message
Muted text
Left aligned
Centered
Right aligned
UPPERCASE
lowercase
Capitalized
This very long text will be cut off with an ellipsis...
```
### Grid & Flex Layout
```html
Column 1
Column 2
Column 3
Centered content
```
### Responsive Display
```html
Desktop navigation
Mobile menu
Responsive heading
```
---
## Using Variables & Mixins
### In React Components with CSS Modules
```scss
// MyComponent.module.scss
@use '@/styles/variables' as *;
@use '@/styles/mixins' as *;
.container {
padding: $space-6;
background-color: $bg-primary;
border-radius: $radius-lg;
@include elevation(2);
}
.title {
@include heading-3;
color: $color-primary;
margin-bottom: $space-4;
}
.description {
@include text-body;
color: $fg-secondary;
line-height: $line-height-relaxed;
}
.buttonGroup {
@include flex-between;
gap: $space-4;
margin-top: $space-6;
}
.button {
@include btn-primary;
&:hover {
@include elevation(3);
}
}
```
```tsx
// MyComponent.tsx
import styles from './MyComponent.module.scss'
export function MyComponent() {
return (
Card Title
Card description
)
}
```
### Standalone SCSS File
```scss
// styles/dashboard.scss
@use '@/styles/variables' as *;
@use '@/styles/mixins' as *;
.dashboard {
@include grid(4);
gap: $space-6;
padding: $space-8;
background-color: $bg-secondary;
min-height: 100vh;
}
.dashboardCard {
@include card;
padding: $space-6;
&:hover {
@include elevation(3);
}
@include breakpoint-md {
@include grid(2);
}
@include breakpoint-sm {
@include grid(1);
}
}
.cardTitle {
@include heading-4;
color: $color-primary;
margin-bottom: $space-4;
}
.cardValue {
@include text-xl;
font-weight: $font-weight-bold;
color: $fg-primary;
margin-bottom: $space-2;
}
.cardMetric {
@include text-sm;
color: $fg-secondary;
}
```
---
## Creating Custom Components
### Form Input Component
```scss
// styles/components/_form-input.scss
@use '@/styles/variables' as *;
@use '@/styles/mixins' as *;
.formInput {
@include input-base;
&-wrapper {
margin-bottom: $space-6;
}
&-label {
display: block;
margin-bottom: $space-2;
font-weight: $font-weight-medium;
color: $fg-primary;
}
&-helper {
display: block;
margin-top: $space-1;
font-size: $font-size-xs;
color: $fg-tertiary;
}
&.error {
border-color: $color-error;
.formInput-helper {
color: $color-error;
}
}
&.success {
border-color: $color-success;
.formInput-helper {
color: $color-success;
}
}
}
```
### Data Table Component
```scss
// styles/components/_data-table.scss
@use '@/styles/variables' as *;
@use '@/styles/mixins' as *;
.dataTable {
width: 100%;
border-collapse: collapse;
margin-bottom: $space-6;
&-header {
background-color: $color-neutral-100;
th {
padding: $space-4;
text-align: left;
font-weight: $font-weight-semibold;
color: $fg-primary;
border-bottom: 2px solid $color-neutral-300;
}
}
&-body {
tr {
transition: background-color $transition-base;
&:hover {
background-color: $color-neutral-50;
}
&:nth-child(even) {
background-color: $color-neutral-50;
}
}
td {
padding: $space-4;
border-bottom: 1px solid $color-neutral-200;
color: $fg-primary;
}
}
}
```
### Modal Overlay Component
```scss
// styles/components/_modal.scss
@use '@/styles/variables' as *;
@use '@/styles/mixins' as *;
.modalBackdrop {
@include position-absolute(0, 0, 0, 0);
background-color: color.change(#000000, $alpha: 0.5);
z-index: $z-modal-backdrop;
display: flex;
align-items: center;
justify-content: center;
}
.modal {
background-color: $bg-primary;
border-radius: $radius-lg;
box-shadow: $shadow-2xl;
z-index: $z-modal;
padding: $space-8;
max-width: 500px;
width: 90%;
@include breakpoint-md {
width: 100%;
}
&-header {
margin-bottom: $space-6;
border-bottom: 1px solid $color-neutral-200;
padding-bottom: $space-4;
h2 {
margin: 0;
@include heading-2;
}
}
&-body {
margin-bottom: $space-6;
}
&-footer {
@include flex-between;
gap: $space-4;
border-top: 1px solid $color-neutral-200;
padding-top: $space-4;
}
}
```
---
## Responsive Design
### Mobile-First Approach
```scss
@use '@/styles/variables' as *;
@use '@/styles/mixins' as *;
.responsiveLayout {
// Mobile-first: default to 1 column
@include grid(1);
gap: $space-4;
padding: $space-4;
// Tablet: 2 columns
@include breakpoint-md {
@include grid(2);
gap: $space-6;
padding: $space-6;
}
// Desktop: 3 columns
@include breakpoint-lg {
@include grid(3);
gap: $space-8;
padding: $space-8;
}
// Large desktop: 4 columns
@include breakpoint-xl {
@include grid(4);
}
}
.responsiveText {
// Mobile: small font
font-size: $font-size-base;
padding: $space-4;
// Tablet: medium font
@include breakpoint-md {
font-size: $font-size-lg;
padding: $space-6;
}
// Desktop: large font
@include breakpoint-lg {
font-size: $font-size-xl;
padding: $space-8;
}
}
.responsiveImage {
max-width: 100%;
height: auto;
@include breakpoint-md {
border-radius: $radius-lg;
@include elevation(2);
}
@include breakpoint-lg {
border-radius: $radius-2xl;
@include elevation(3);
}
}
```
---
## Theme Switching
### In JavaScript
```javascript
// Switch to dark theme
document.documentElement.setAttribute('data-theme', 'dark')
// Switch to light theme
document.documentElement.removeAttribute('data-theme')
// or
document.documentElement.setAttribute('data-theme', 'light')
// Get current theme
const currentTheme = document.documentElement.getAttribute('data-theme')
// Check system preference
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches
if (prefersDark) {
document.documentElement.setAttribute('data-theme', 'dark')
}
```
### In React
```tsx
import { useEffect, useState } from 'react'
export function ThemeToggle() {
const [theme, setTheme] = useState<'light' | 'dark'>('light')
useEffect(() => {
const root = document.documentElement
if (theme === 'dark') {
root.setAttribute('data-theme', 'dark')
} else {
root.removeAttribute('data-theme')
}
}, [theme])
return (
)
}
```
### Using CSS Variables
```css
.themeAwareElement {
background-color: var(--bg-primary);
color: var(--fg-primary);
font-family: var(--font-family-body);
transition: background-color var(--transition-base);
}
.themeAwareBorder {
border-color: var(--color-neutral-300);
}
[data-theme='dark'] .themeAwareBorder {
border-color: var(--color-neutral-700);
}
```
---
## Advanced Techniques
### Extending Mixins
```scss
@use '@/styles/variables' as *;
@use '@/styles/mixins' as *;
// Custom button with additional styling
@mixin btn-custom {
@include btn-primary;
border-radius: $radius-full;
padding: $space-3 $space-6;
font-weight: $font-weight-semibold;
letter-spacing: 0.5px;
text-transform: uppercase;
}
.customButton {
@include btn-custom;
}
```
### Creating Color Variants
```scss
@use '@/styles/variables' as *;
@mixin card-variant($bg-color, $border-color) {
background-color: $bg-color;
border: 2px solid $border-color;
padding: $space-6;
border-radius: $radius-lg;
}
.card-success {
@include card-variant(
oklch(0.93 0.12 149.47), // Light green
$color-success
);
}
.card-warning {
@include card-variant(
oklch(0.95 0.15 70.08), // Light yellow
$color-warning
);
}
.card-error {
@include card-variant(
oklch(0.92 0.2 25.39), // Light red
$color-error
);
}
```
### Dynamic Spacing Loop
```scss
@use '@/styles/variables' as *;
// Create responsive spacing utilities
.spacing {
@for $i from 1 through 6 {
$value: $space-4 * $i;
&-#{$i} {
padding: $value;
}
@include breakpoint-md {
&-md-#{$i} {
padding: $value * 1.5;
}
}
@include breakpoint-lg {
&-lg-#{$i} {
padding: $value * 2;
}
}
}
}
```
### Conditional Color Adjustments
```scss
@use 'sass:color' as *;
@use '@/styles/variables' as *;
.accentElement {
background-color: $color-primary;
@media (prefers-contrast: more) {
// Increase contrast for high contrast mode
background-color: change-color($color-primary, $lightness: 40%);
color: #ffffff;
}
}
```
---
## Best Practices
✓ Always use variables instead of hardcoded values
✓ Use mixins for frequently used patterns
✓ Keep components responsive with breakpoint mixins
✓ Test color accessibility
✓ Use CSS variables for runtime theme switching
✓ Document custom mixins and variables
✓ Keep nesting 3 levels deep maximum
✓ Use meaningful class names
---
See [SASS_CONFIGURATION.md](./SASS_CONFIGURATION.md) for comprehensive reference.