ContextMenu.vue 5.3 KB
Newer Older
V
Vben 已提交
1 2
<script lang="tsx">
  import type { ContextMenuItem, ItemContentProps, Axis } from './typing';
3
  import type { FunctionalComponent, CSSProperties, PropType } from 'vue';
V
Vben 已提交
4
  import { defineComponent, nextTick, onMounted, computed, ref, unref, onUnmounted } from 'vue';
5
  import { Icon } from '/@/components/Icon';
V
Vben 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
  import { Menu, Divider } from 'ant-design-vue';

  const prefixCls = 'context-menu';

  const props = {
    width: { type: Number, default: 156 },
    customEvent: { type: Object as PropType<Event>, default: null },
    styles: { type: Object as PropType<CSSProperties> },
    showIcon: { type: Boolean, default: true },
    axis: {
      // The position of the right mouse button click
      type: Object as PropType<Axis>,
      default() {
        return { x: 0, y: 0 };
      },
    },
    items: {
      // The most important list, if not, will not be displayed
      type: Array as PropType<ContextMenuItem[]>,
      default() {
        return [];
      },
    },
  };

  const ItemContent: FunctionalComponent<ItemContentProps> = (props) => {
    const { item } = props;
    return (
      <span
        style="display: inline-block; width: 100%; "
        class="px-4"
        onClick={props.handler.bind(null, item)}
      >
        {props.showIcon && item.icon && <Icon class="mr-2" icon={item.icon} />}
        <span>{item.label}</span>
      </span>
    );
  };

  export default defineComponent({
    name: 'ContextMenu',
    props,
    setup(props) {
49
      const wrapRef = ref(null);
V
Vben 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62
      const showRef = ref(false);

      const getStyle = computed((): CSSProperties => {
        const { axis, items, styles, width } = props;
        const { x, y } = axis || { x: 0, y: 0 };
        const menuHeight = (items || []).length * 40;
        const menuWidth = width;
        const body = document.body;

        const left = body.clientWidth < x + menuWidth ? x - menuWidth : x;
        const top = body.clientHeight < y + menuHeight ? y - menuHeight : y;
        return {
          ...styles,
V
vben 已提交
63
          position: 'absolute',
V
Vben 已提交
64 65 66
          width: `${width}px`,
          left: `${left + 1}px`,
          top: `${top + 1}px`,
67
          zIndex: 9999,
V
Vben 已提交
68 69 70 71 72 73 74 75
        };
      });

      onMounted(() => {
        nextTick(() => (showRef.value = true));
      });

      onUnmounted(() => {
V
Vben 已提交
76 77
        const el = unref(wrapRef);
        el && document.body.removeChild(el);
V
Vben 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91
      });

      function handleAction(item: ContextMenuItem, e: MouseEvent) {
        const { handler, disabled } = item;
        if (disabled) {
          return;
        }
        showRef.value = false;
        e?.stopPropagation();
        e?.preventDefault();
        handler?.();
      }

      function renderMenuItem(items: ContextMenuItem[]) {
92 93
        const visibleItems = items.filter((item) => !item.hidden);
        return visibleItems.map((item) => {
V
Vben 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
          const { disabled, label, children, divider = false } = item;

          const contentProps = {
            item,
            handler: handleAction,
            showIcon: props.showIcon,
          };

          if (!children || children.length === 0) {
            return (
              <>
                <Menu.Item disabled={disabled} class={`${prefixCls}__item`} key={label}>
                  <ItemContent {...contentProps} />
                </Menu.Item>
                {divider ? <Divider key={`d-${label}`} /> : null}
              </>
            );
          }
          if (!unref(showRef)) return null;

          return (
            <Menu.SubMenu key={label} disabled={disabled} popupClassName={`${prefixCls}__popup`}>
              {{
                title: () => <ItemContent {...contentProps} />,
                default: () => renderMenuItem(children),
              }}
            </Menu.SubMenu>
          );
        });
      }
      return () => {
V
Vben 已提交
125 126 127
        if (!unref(showRef)) {
          return null;
        }
V
Vben 已提交
128 129
        const { items } = props;
        return (
V
vben 已提交
130 131 132 133 134
          <div class={prefixCls}>
            <Menu inlineIndent={12} mode="vertical" ref={wrapRef} style={unref(getStyle)}>
              {renderMenuItem(items)}
            </Menu>
          </div>
V
Vben 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
        );
      };
    },
  });
</script>
<style lang="less">
  @default-height: 42px !important;

  @small-height: 36px !important;

  @large-height: 36px !important;

  .item-style() {
    li {
      display: inline-block;
      width: 100%;
      height: @default-height;
      margin: 0 !important;
      line-height: @default-height;

      span {
        line-height: @default-height;
      }

      > div {
        margin: 0 !important;
      }

      &:not(.ant-menu-item-disabled):hover {
        background-color: @item-hover-bg;
V
vben 已提交
165
        color: @text-color-base;
V
Vben 已提交
166 167 168 169 170
      }
    }
  }

  .context-menu {
V
vben 已提交
171
    display: block;
V
Vben 已提交
172
    position: fixed;
V
vben 已提交
173
    z-index: 200;
V
Vben 已提交
174 175 176 177
    top: 0;
    left: 0;
    width: 156px;
    margin: 0;
V
vben 已提交
178
    border: 1px solid rgb(0 0 0 / 8%);
V
Vben 已提交
179
    border-radius: 0.25rem;
V
vben 已提交
180 181
    background-clip: padding-box;
    background-color: @component-background;
V
vben 已提交
182 183
    box-shadow: 0 2px 2px 0 rgb(0 0 0 / 14%), 0 3px 1px -2px rgb(0 0 0 / 10%),
      0 1px 5px 0 rgb(0 0 0 / 6%);
V
vben 已提交
184
    list-style: none;
V
Vben 已提交
185 186
    user-select: none;

V
vben 已提交
187 188 189
    &__item {
      margin: 0 !important;
    }
V
Vben 已提交
190 191 192
    .item-style();

    .ant-divider {
V
vben 已提交
193
      margin: 0;
V
Vben 已提交
194 195 196 197
    }

    &__popup {
      .ant-divider {
V
vben 已提交
198
        margin: 0;
V
Vben 已提交
199 200 201 202 203 204 205 206 207 208 209
      }

      .item-style();
    }

    .ant-menu-submenu-title,
    .ant-menu-item {
      padding: 0 !important;
    }
  }
</style>