跳到内容
SCRM DOC
HooksVue3DesignCSS创建于2026-01-10更新于 2026-04-22

useDesign 设计系统 Hook

概述

useDesign 提供统一的设计系统前缀,确保组件样式的一致性和可维护性。

导入方式

import { useDesign } from '@/hooks/web';

使用方式

const { prefixCls, prefixVar } = useDesign(scope: string);

返回值

interface UseDesignReturn {
  prefixCls: string;  // 组件前缀类名
  prefixVar: string;  // 全局前缀变量
}

参数说明

参数说明类型必填
scope作用域名称,通常是组件名称string是

基础用法

<template>
  <div :class="`${prefixCls}`">
    <div :class="`${prefixCls}-header`">头部</div>
    <div :class="`${prefixCls}-body`">内容</div>
    <div :class="`${prefixCls}-footer`">底部</div>
  </div>
</template>

<script setup lang="ts">
import { useDesign } from '@/hooks/web';

const { prefixCls } = useDesign('my-component');
// prefixCls 的值为 'ant-design-my-component'
</script>

<style scoped>
.ant-design-my-component {
  border: 1px solid #d9d9d9;
  border-radius: 4px;
}

.ant-design-my-component-header {
  padding: 12px 16px;
  background: #fafafa;
  border-bottom: 1px solid #d9d9d9;
}

.ant-design-my-component-body {
  padding: 16px;
}

.ant-design-my-component-footer {
  padding: 12px 16px;
  border-top: 1px solid #d9d9d9;
}
</style>

CSS 变量使用

<template>
  <div :class="`${prefixCls}`">
    <div :class="`${prefixCls}-title`">标题</div>
  </div>
</template>

<script setup lang="ts">
import { useDesign } from '@/hooks/web';

const { prefixCls, prefixVar } = useDesign('feature');
// prefixCls: 'ant-design-feature'
// prefixVar: 'ant-design'
</script>

<style scoped>
/* 使用前缀变量定义 CSS 变量 */
.ant-design-feature {
  --ant-design-primary-color: #1890ff;
  color: var(--ant-design-primary-color);
}

/* 或者使用 CSS 变量 */
.ant-design-feature-title {
  color: var(--ant-design-primary-color);
  font-size: 16px;
}
</style>

动态类名构建

<template>
  <div :class="getClass('container')">
    <div :class="getClass('header')">{{ title }}</div>
    <div :class="getClass('content')">
      <slot></slot>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useDesign } from '@/hooks/web';

const { prefixCls } = useDesign('card');

const title = ref('卡片标题');

// 工具函数:构建类名
const getClass = (suffix: string) => {
  return `${prefixCls}-${suffix}`;
};
</script>

<style scoped>
.ant-design-card-container {
  border: 1px solid #e8e8e8;
}

.ant-design-card-header {
  padding: 16px;
  border-bottom: 1px solid #e8e8e8;
}

.ant-design-card-content {
  padding: 24px;
}
</style>

组合使用

<template>
  <div :class="`${prefixCls} ${prefixCls}-modal`">
    <a-modal
      v-bind="$attrs"
      :class="`${prefixCls}-modal-wrapper`"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <template #title>
        <div :class="`${prefixCls}-title`">
          <Icon :icon="icon" v-if="icon" />
          <span>{{ title }}</span>
        </div>
      </template>

      <div :class="`${prefixCls}-body`">
        <slot></slot>
      </div>

      <template #footer>
        <div :class="`${prefixCls}-footer`">
          <slot name="footer"></slot>
        </div>
      </template>
    </a-modal>
  </div>
</template>

<script setup lang="ts">
import { useDesign } from '@/hooks/web';

interface Props {
  title?: string;
  icon?: string;
}

const props = defineProps<Props>();
const emit = defineEmits(['ok', 'cancel']);

const { prefixCls } = useDesign('basic-modal');

const handleOk = () => emit('ok');
const handleCancel = () => emit('cancel');
</script>

<style scoped>
.ant-design-basic-modal {
  /* 样式定义 */
}

.ant-design-basic-modal-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
  font-weight: 500;
}

.ant-design-basic-modal-body {
  padding: 24px;
}

.ant-design-basic-modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}
</style>

BEM 命名规范

使用 useDesign 配合 BEM (Block Element Modifier) 命名规范:

<template>
  <div :class="bem()"> <!-- Block -->
    <div :class="bem('header')"> <!-- Element -->
      <div :class="bem('title', 'primary')"> <!-- Modifier -->
        标题
      </div>
    </div>
    <div :class="bem('body')">
      <div :class="bem('content', ['active', 'large'])">
        内容
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useDesign } from '@/hooks/web';
import { computed } from 'vue';

const { prefixCls } = useDesign('component');

// BEM 工具函数
const bem = (element?: string, modifier?: string | string[]) => {
  const base = prefixCls;
  let className = base;

  if (element) {
    className += `__${element}`;
  }

  if (modifier) {
    if (Array.isArray(modifier)) {
      modifier.forEach(m => {
        className += ` ${base}--${m}`;
      });
    } else {
      className += ` ${base}--${modifier}`;
    }
  }

  return className;
};
</script>

<style scoped>
/* Block */
.ant-design-component {}

/* Element */
.ant-design-component__header {}
.ant-design-component__body {}
.ant-design-component__content {}

/* Modifier */
.ant-design-component--active {}
.ant-design-component--large {}
.ant-design-component__title--primary {}
</style>

完整组件示例

<template>
  <div :class="prefixCls">
    <div :class="`${prefixCls}-header`">
      <div :class="`${prefixCls}-title`">
        <Icon :icon="titleIcon" v-if="titleIcon" />
        <span>{{ title }}</span>
      </div>
      <div :class="`${prefixCls}-extra`">
        <slot name="extra"></slot>
      </div>
    </div>

    <div :class="`${prefixCls}-body`" :style="bodyStyle">
      <slot></slot>
    </div>

    <div :class="`${prefixCls}-footer`" v-if="$slots.footer">
      <slot name="footer"></slot>
    </div>
  </div>
</template>

<script setup lang="ts">
import { useDesign } from '@/hooks/web';

interface Props {
  title?: string;
  titleIcon?: string;
  bodyStyle?: Record<string, any>;
}

defineProps<Props>();
const { prefixCls } = useDesign('card');
</script>

<style scoped>
.ant-design-card {
  background: #fff;
  border-radius: 2px;
  box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.03);
}

.ant-design-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 24px;
  border-bottom: 1px solid #f0f0f0;
}

.ant-design-card-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
  font-weight: 500;
  line-height: 22px;
}

.ant-design-card-body {
  padding: 24px;
}

.ant-design-card-footer {
  padding: 12px 24px;
  border-top: 1px solid #f0f0f0;
}
</style>

主题定制

<template>
  <div :class="`${prefixCls} ${prefixCls}-${theme}`">
    <div :class="`${prefixCls}-inner`">
      内容
    </div>
  </div>
</template>

<script setup lang="ts">
import { useDesign } from '@/hooks/web';

interface Props {
  theme?: 'light' | 'dark';
}

const props = withDefaults(defineProps<Props>(), {
  theme: 'light',
});

const { prefixCls } = useDesign('themed-component');
</script>

<style scoped>
/* 默认主题 */
.ant-design-themed-component {
  background: #fff;
  color: #000;
}

/* 深色主题 */
.ant-design-themed-component--dark {
  background: #1f1f1f;
  color: #fff;
}

/* 使用 CSS 变量 */
.ant-design-themed-component-inner {
  background: var(--ant-design-bg-color);
  color: var(--ant-design-text-color);
}
</style>

注意事项

  1. 前缀一致性:所有组件使用统一前缀 ant-design
  2. 命名规范:使用 kebab-case (短横线) 命名
  3. 作用域隔离:使用 scoped 样式避免污染
  4. CSS 变量:利用 CSS 变量实现主题定制
  5. 类名构建:使用模板字符串构建类名

相关文档


文档版本: 1.0.1 最后更新: 2026-04-22

本页目录