npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ophiuchus/dropdown

v1.0.1

Published

### 介绍

Downloads

2

Readme

DropdownMenu 下拉菜单

介绍

向下弹出的菜单列表。

引入

// 方式1(推荐)
import Vue from 'vue';
import Dropdown from '@ophiuchus/dropdown';

Vue.use(Dropdown);

// 方式2
import Vue from 'vue';
import { DropdownMenu, DropdownItem } from '@ophiuchus/dropdown';

Vue.component(DropdownMenu.name, DropdownMenu);
Vue.component(DropdownItem.name, DropdownItem);

代码演示

基础用法

<sf-dropdown-menu>
  <sf-dropdown-item v-model="value1" :options="option1" />
  <sf-dropdown-item v-model="value2" :options="option2" />
</sf-dropdown-menu>
export default {
  data() {
    return {
      value1: 0,
      value2: 'a',
      option1: [
        { text: '全部商品', value: 0 },
        { text: '新款商品', value: 1 },
        { text: '活动商品', value: 2 },
      ],
      option2: [
        { text: '默认排序', value: 'a' },
        { text: '好评排序', value: 'b' },
        { text: '销量排序', value: 'c' },
      ],
    };
  },
};

自定义菜单内容

通过插槽可以自定义 DropdownItem 的内容,此时需要使用实例上的 toggle 方法手动控制菜单的显示。

<sf-dropdown-menu>
  <sf-dropdown-item v-model="value" :options="option" />
  <sf-dropdown-item title="筛选" ref="item">
    <sf-cell center title="包邮">
      <template #right-icon>
        <sf-switch v-model="switch1" size="24" active-color="#ee0a24" />
      </template>
    </sf-cell>
    <sf-cell center title="团购">
      <template #right-icon>
        <sf-switch v-model="switch2" size="24" active-color="#ee0a24" />
      </template>
    </sf-cell>
    <div style="padding: 5px 16px;">
      <sf-button type="danger" block round @click="onConfirm">
        确认
      </sf-button>
    </div>
  </sf-dropdown-item>
</sf-dropdown-menu>
export default {
  data() {
    return {
      value: 0,
      switch1: false,
      switch2: false,
      option: [
        { text: '全部商品', value: 0 },
        { text: '新款商品', value: 1 },
        { text: '活动商品', value: 2 },
      ],
    };
  },
  methods: {
    onConfirm() {
      this.$refs.item.toggle();
    },
  },
};

自定义选中态颜色

通过 active-color 属性可以自定义菜单标题和选项的选中态颜色。

<sf-dropdown-menu active-color="#1989fa">
  <sf-dropdown-item v-model="value1" :options="option1" />
  <sf-dropdown-item v-model="value2" :options="option2" />
</sf-dropdown-menu>

向上展开

direction 属性值设置为 up,菜单即可向上展开。

<sf-dropdown-menu direction="up">
  <sf-dropdown-item v-model="value1" :options="option1" />
  <sf-dropdown-item v-model="value2" :options="option2" />
</sf-dropdown-menu>

禁用菜单

<sf-dropdown-menu>
  <sf-dropdown-item v-model="value1" disabled :options="option1" />
  <sf-dropdown-item v-model="value2" disabled :options="option2" />
</sf-dropdown-menu>

API

DropdownMenu Props

| 参数 | 说明 | 类型 | 默认值 | | --- | --- | --- | --- | | active-color | 菜单标题和选项的选中态颜色 | string | #ee0a24 | | direction | 菜单展开方向,可选值为up | string | down | | z-index | 菜单栏 z-index 层级 | number | string | 10 | | duration | 动画时长,单位秒 | number | string | 0.2 | | overlay | 是否显示遮罩层 | boolean | true | | close-on-click-overlay | 是否在点击遮罩层后关闭菜单 | boolean | true | | close-on-click-outside | 是否在点击外部元素后关闭菜单 | boolean | true |

DropdownItem Props

| 参数 | 说明 | 类型 | 默认值 | | --- | --- | --- | --- | | value | 当前选中项对应的 value,可以通过v-model双向绑定 | number | string | - | | title | 菜单项标题 | string | 当前选中项文字 | | options | 选项数组 | Option[] | [] | | disabled | 是否禁用菜单 | boolean | false | | lazy-render | 是否在首次展开时才渲染菜单内容 | boolean | true | | title-class | 标题额外类名 | string | - | | get-container | 指定挂载的节点,用法示例 | string | () => Element | - |

DropdownItem Events

| 事件名 | 说明 | 回调参数 | | ------ | ----------------------------- | -------- | | change | 点击选项导致 value 变化时触发 | value | | open | 打开菜单栏时触发 | - | | close | 关闭菜单栏时触发 | - | | opened | 打开菜单栏且动画结束后触发 | - | | closed | 关闭菜单栏且动画结束后触发 | - |

DropdownItem Slots

| 名称 | 说明 | | ------- | ---------------- | | default | 菜单内容 | | title | 自定义菜单项标题 |

DropdownItem 方法

通过 ref 可以获取到 DropdownItem 实例并调用实例方法,详见组件实例方法

| 方法名 | 说明 | 参数 | 返回值 | | --- | --- | --- | --- | | toggle | 切换菜单展示状态,传 true 为显示,false 为隐藏,不传参为取反 | show?: boolean | - |

Option 数据结构

| 键名 | 说明 | 类型 | | ----- | -------------------------------------- | ------------------ | | text | 文字 | string | | value | 标识符 | number | string | | icon | 左侧图标名称或图片链接 | string |

样式变量

组件提供了下列 Less 变量,可用于自定义样式,使用方法请参考主题定制

| 名称 | 默认值 | 描述 | | --- | --- | --- | | @dropdown-menu-height | 48px | - | | @dropdown-menu-background-color | @white | - | | @dropdown-menu-box-shadow | 0 2px 12px fade(@gray-7, 12) | - | | @dropdown-menu-title-font-size | 15px | - | | @dropdown-menu-title-text-color | @text-color | - | | @dropdown-menu-title-active-text-color | @red | - | | @dropdown-menu-title-disabled-text-color | @gray-6 | - | | @dropdown-menu-title-padding | 0 @padding-xs | - | | @dropdown-menu-title-line-height | @line-height-lg | - | | @dropdown-menu-option-active-color | @red | - | | @dropdown-menu-content-max-height | 80% | - | | @dropdown-item-z-index | 10 | - |

常见问题

父元素设置 transform 后,下拉菜单的位置错误?

DropdownMenu 嵌套在 Tabs 等组件内部使用时,可能会遇到下拉菜单位置错误的问题。这是因为在 Chrome 浏览器中,transform 元素内部的 fixed 布局会降级成 absolute 布局,导致下拉菜单的布局异常。

DropdownItemget-container 属性设置为 body 即可避免此问题:

<sf-dropdown-menu>
  <sf-dropdown-item get-container="body" />
  <sf-dropdown-item get-container="body" />
</sf-dropdown-menu>