
English
The project uses UnoCSS + SCSS styling approach:
Configuration file: uno.config.ts
// uno.config.ts
import {
defineConfig,
presetUno,
presetAttributify,
presetIcons,
} from 'unocss'
import presetWot from '@wot-ui/unocss-preset'
export default defineConfig({
presets: [
presetUno(), // Default preset (Tailwind compatible)
presetAttributify(), // Attribute mode
presetIcons(), // Icon support
presetWot(), // wot-ui preset
],
// Custom shortcuts
shortcuts: {
// Center flex
'flex-center': 'flex items-center justify-center',
// Space between
'flex-between': 'flex items-center justify-between',
// Common text styles
'text-primary': 'text-[--color-primary]',
'text-secondary': 'text-gray-500',
},
})<template>
<!-- Flex layout -->
<view class="flex items-center justify-between p-4">
<text class="text-lg font-bold">Title</text>
<button text-sm>Button</button>
</view>
<!-- Grid layout -->
<view class="grid grid-cols-3 gap-4">
<view class="item">1</view>
<view class="item">2</view>
<view class="item">3</view>
</view>
</template>.container {
/* Fixed width */
w-full; /* 100% */
w-screen; /* 100vw */
/* Fixed height */
h-full; /* 100% */
h-screen; /* 100vh */
/* Custom sizes */
w-100px;
h-50%;
/* Max/min sizes */
max-w-screen-md;
min-h-screen;
}/* Padding */
p-4; /* padding: 1rem */
px-4; /* padding-left/right: 1rem */
py-2; /* padding-top/bottom: 0.5rem */
/* Margin */
m-4; /* margin: 1rem */
mt-4; /* margin-top: 1rem */
mb-auto; /* margin-bottom: auto */
/* Gap (Flex/Grid child spacing) */
gap-2; /* gap: 0.5rem */
gap-x-4; /* column-gap: 1rem *//* Size */
text-xs; /* 0.75rem */
text-sm; /* 0.875rem */
text-base; /* 1rem */
text-lg; /* 1.125rem */
text-xl; /* 1.25rem */
text-2xl; /* 1.5rem */
/* Weight */
font-normal; /* 400 */
font-medium; /* 500 */
font-semibold; /* 600 */
font-bold; /* 700 */
/* Alignment */
text-left;
text-center;
text-right;
/* Other */
leading-tight; /* Tight line height */
leading-relaxed; /* Loose line height */
tracking-wide; /* Letter spacing */
truncate; /* Text truncation with ellipsis *//* Named colors */
text-red-500;
bg-blue-100;
border-gray-300;
/* Opacity */
text-black/50; /* rgba(0,0,0,0.5) */
bg-white/80;
/* CSS variables */
text-[--color-primary];
bg-[--bg-color];
/* Arbitrary values */
text-[#1890ff];
bg-[rgb(24,144,255)];:border; /* 1px solid currentColor */
border-2; /* 2px solid */
border-t; /* Top border */
border-dashed; /* Dashed */
rounded; /* Border radius */
rounded-full; /* Circle */
rounded-lg; /* Large radius *//* Display */
:block;
:inline-block;
:inline-flex;
:hidden; /* display: none */
/* Positioning */
:relative;
:absolute;
:fixed;
:sticky;
top-0;
left-0;
right-0;
bottom-0;
/* Overflow */
overflow-hidden;
overflow-auto;
overflow-x-auto;/* Mobile adaptation */
sm:text-base; /* >= 640px */
md:text-lg; /* >= 768px */
lg:text-xl; /* >= 1024px */
xl:text-2xl; /* >= 1280px */Define variables in uni.scss or components:
/* uni.scss */
$primary-color: #1890ff;
$success-color: #52c41a;
$warning-color: #faad14;
$error-color: #f5222d;
$text-color: #333333;
$text-color-secondary: #666666;
$border-color: #d9d9d9;
$bg-color: #f5f5f5;
/* Safe area */
$safe-area-inset-bottom: constant(safe-area-inset-bottom);
$safe-area-inset-bottom-env: env(safe-area-inset-bottom);<style lang="scss" scoped>
.container {
background-color: $bg-color;
border: 1px solid $border-color;
padding: $spacing-md;
.title {
color: $text-color;
font-size: $font-size-lg;
font-weight: bold;
}
&.active {
background-color: $primary-color;
color: white;
}
}
</style>Common Mixin examples:
/* mixins.scss */
@mixin flex-center {
display: flex;
align-items: center;
justify-content: center;
}
@mixin text-ellipsis($lines: 1) {
@if $lines == 1 {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
} @else {
display: -webkit-box;
-webkit-line-clamp: $lines;
-webkit-box-orient: vertical;
overflow: hidden;
}
}
@mixin safe-area-bottom {
padding-bottom: $safe-area-inset-bottom;
padding-bottom: $safe-area-inset-bottom-env;
}
/* Usage */
.card {
@include flex-center;
.title {
@include text-ellipsis(2);
}
}Customize wot-ui component styles via CSS variables:
:root {
--color-primary: #1890ff;
--color-success: #52c41a;
--color-warning: #faad14;
--color-danger: #ff4d4f;
--navbar-height: 44px;
--tabbar-height: 50px;
}<style scoped>
/* Override wot-ui internal styles (requires :deep()) */
::deep(.wd-button) {
border-radius: 8px;
}
::deep(.wd-navbar__title) {
font-weight: 600;
}
</style>UniApp-specific unit, adapts to screen width:
.box {
width: 750rpx; /* Relative to screen width */
height: 200rpx;
font-size: 28rpx;
padding: 30rpx;
}Design draft width 750rpx, conversion relationship:
Bottom safe area adaptation:
.page-content {
padding-bottom: calc(20rpx + constant(safe-area-inset-bottom));
padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
}Use UnoCSS atomic classes for simple styles:
<!-- Recommended -->
<view class="flex items-center p-4 bg-white rounded-lg shadow">
<!-- Not recommended -->
<view class="container">Use SCSS when nesting, loops, conditionals are needed:
.list-item {
&:nth-child(even) {
background-color: #fafafa;
}
&.active {
.title {
color: $primary-color;
}
}
}Use BEM convention for complex components:
/* Block */
.card {}
/* Element */
.card__header {}
.card__body {}
.card__footer {}
/* Modifier */
.card--primary {}
.card--hidden {}Limit scoped style depth for better performance:
/* Selector too deep */
.parent .child .grand-child .target {}
/* Better, flatter */
.target {}Use min-width media queries for mobile-first design:
/* Base styles (mobile) */
.container {
padding: 16px;
}
/* Tablet and above */
@media (min-width: 768px) {
.container {
padding: 32px;
}
}