naive-ui/demo/documentation/components/dropdown/enUS/manualPosition.md
2019-12-03 20:00:29 +08:00

1.7 KiB

Manually Positioned

For some special case, you may want to manually position the dropdown. For example, right click to activate dropdown in some area.

<div style="width: 200px; height: 200px; background-color: rgba(0, 128, 0, .5);" @contextmenu="handleContextMenu">
  Right Click
</div>
<n-dropdown
  placement="bottom-start"
  trigger="manual"
  manually-positioned
  @select="handleSelect"
  @blur="handleBlur"
  :x="x"
  :y="y"
  v-model="showDropdown"
>
  <n-dropdown-item name="gatsby">
    Gatsby
  </n-dropdown-item>
  <n-dropdown-item name="daisy">
    Daisy
  </n-dropdown-item>
  <n-dropdown-divider />
  <n-dropdown-item name="nick">
    Nick
  </n-dropdown-item>
  <n-dropdown-submenu>
    <template v-slot:activator>
      Others
    </template>
    <n-dropdown-item name="jordan baker">
      Jordan Baker
    </n-dropdown-item>
    <n-dropdown-divider />
    <n-dropdown-item name="tom buchanan">
      Tom Buchanan
    </n-dropdown-item>
    <n-dropdown-submenu>
      <template v-slot:activator>
        Others
      </template>
      <n-dropdown-item name="chicken">
        Chicken
      </n-dropdown-item>
      <n-dropdown-item name="beef">
        Beef
      </n-dropdown-item>
    </n-dropdown-submenu>
  </n-dropdown-submenu>
</n-dropdown>
export default {
  methods: {
    handleSelect (name) {
      this.showDropdown = false
      this.$NMessage.info(name)
    },
    handleBlur () {
      this.showDropdown = false
    },
    handleContextMenu (e) {
      e.preventDefault()
      this.showDropdown = false
      this.$nextTick().then(() => {
        this.showDropdown = true
        this.x = e.clientX
        this.y = e.clientY
      })
    }
  },
  data () {
    return {
      showDropdown: false,
      x: 0,
      y: 0
    }
  }
}