Skip to content

基础用法

可以借助全局配置组件,来进行样式覆盖

[default]默认按钮
[plain]简约按钮
[round]圆角按钮
[dashed]虚线按钮
<template>
  <b-config-provider
    :theme="{
      binColorPrimary: '#722ed1',
      binColorSuccess: '#13c2c2',
      binColorWarning: '#e14c19',
      binColorDanger: '#d42a87'
    }"
  >
    <div>
      <div>[default]默认按钮</div>
      <div class="demo-button">
        <b-button type="primary">Primary</b-button>
        <b-button type="success">Success</b-button>
        <b-button type="warning">Warning</b-button>
        <b-button type="danger">Danger</b-button>
      </div>
      <div>[plain]简约按钮</div>
      <div class="demo-button">
        <b-button type="primary" plain>Primary</b-button>
        <b-button type="success" plain>Success</b-button>
        <b-button type="warning" plain>Warning</b-button>
        <b-button type="danger" plain>Danger</b-button>
      </div>
      <div>[round]圆角按钮</div>
      <div class="demo-button">
        <b-button type="primary" round>Primary</b-button>
        <b-button type="success" round>Success</b-button>
        <b-button type="warning" round>Warning</b-button>
        <b-button type="danger" round>Danger</b-button>
      </div>
      <div>[dashed]虚线按钮</div>
      <div class="demo-button">
        <b-button type="primary" dashed>Primary</b-button>
        <b-button type="success" dashed>Success</b-button>
        <b-button type="warning" dashed>Warning</b-button>
        <b-button type="danger" dashed>Danger</b-button>
      </div>
    </div>
  </b-config-provider>
</template>

<style scoped>
.demo-button {
  margin: 8px 0;
}
</style>

抽象容器

可以使用抽象标签来注入,这里会默认注入到html ,进行全局覆盖(谨慎使用,避免冲突和覆盖,一般用于全局配置主题使用)

<template>
  <b-config-provider abstract :theme="config">
    <b-button type="primary" @click="setGlobal">
      点击应用(应用后主颜色会变更,会影响全局)
    </b-button>
  </b-config-provider>
</template>

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

const config = ref({})

function setGlobal() {
  config.value = { binColorPrimary: '#00c181' }
}
</script>

黑色模式

通过 theme-name="dark" 开启黑色模式,支持运行时切换,并自动同步 Teleport 组件和函数式组件(如 Message、Notice、MessageBox)。

<template>
  <b-config-provider :theme-name="themeName">
    <div class="demo-dark">
      <b-button size="small" @click="toggleTheme">
        切换为{{ themeName === 'dark' ? '浅色' : '深色' }}模式
      </b-button>

      <div class="demo-dark__row">
        <b-input v-model="keyword" placeholder="输入关键字" clearable style="width: 220px" />
        <b-select v-model="city" placeholder="请选择城市" style="width: 220px">
          <b-option label="上海" value="shanghai" />
          <b-option label="北京" value="beijing" />
          <b-option label="深圳" value="shenzhen" />
        </b-select>
        <b-date-picker v-model="date" type="date" placeholder="选择日期" style="width: 220px" />
      </div>

      <div class="demo-dark__row">
        <b-dropdown @command="handleCommand">
          <b-button>
            Dropdown
            <i class="b-iconfont b-icon-down"></i>
          </b-button>
          <template #dropdown>
            <b-dropdown-menu>
              <b-dropdown-item name="a">菜单 A</b-dropdown-item>
              <b-dropdown-item name="b">菜单 B</b-dropdown-item>
            </b-dropdown-menu>
          </template>
        </b-dropdown>

        <b-button @click="showMessage">Message</b-button>
        <b-button @click="showNotice">Notice</b-button>
        <b-button @click="showConfirm">MessageBox</b-button>
      </div>
    </div>
  </b-config-provider>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { Message, Notice, MessageBox } from 'bin-ui-design'

const themeName = ref<'light' | 'dark'>('light')
const keyword = ref('')
const city = ref('')
const date = ref('')

const toggleTheme = () => {
  themeName.value = themeName.value === 'dark' ? 'light' : 'dark'
}

const handleCommand = value => {
  Message.info(`点击了菜单: ${value}`)
}

const showMessage = () => {
  Message.success('函数式 Message 已跟随主题')
}

const showNotice = () => {
  Notice.info({
    title: '主题联动',
    message: 'Notice 已跟随 ConfigProvider 主题切换'
  })
}

const showConfirm = () => {
  MessageBox.confirm({
    title: '主题联动',
    message: 'MessageBox 已跟随 ConfigProvider 主题切换'
  })
}
</script>

<style scoped>
.demo-dark {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.demo-dark__row {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}
</style>

国际化

可通过 locale 属性切换语言,当前支持 zh-CNen-US

国际化能力建议在业务项目外部统一管理(例如由应用层 i18n 方案负责),组件库侧通过 ConfigProvider 进行语言注入。

更多说明请参见 国际化

<template>
  <div>
    <div class="locale-actions">
      <b-button size="small" :type="locale === 'zh-CN' ? 'primary' : 'default'" @click="locale = 'zh-CN'">
        中文
      </b-button>
      <b-button size="small" :type="locale === 'en-US' ? 'primary' : 'default'" @click="locale = 'en-US'">
        English
      </b-button>
    </div>

    <b-config-provider :locale="locale">
      <div class="locale-demo-wrap">
        <b-date-picker v-model="date" type="date" />
        <b-time-picker v-model="time" />
      </div>
    </b-config-provider>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'

const locale = ref<'zh-CN' | 'en-US'>('zh-CN')
const date = ref(new Date())
const time = ref(new Date())
</script>

<style scoped>
.locale-actions {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.locale-demo-wrap {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}
</style>