Skip to content

Commit

Permalink
feat: add directive for hover (#620)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeJim committed Apr 24, 2023
1 parent bd8bba0 commit fcd86b5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/cell/cell.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div :class="styleCell" @click="onClick">
<div v-hover="{ className: `${name}--hover`, active: hover }" :class="styleCell" @click="onClick">
<div :class="`${name}__left`">
<div v-if="leftIconContent" :class="`${name}__left-icon`">
<t-node :content="leftIconContent" />
Expand Down Expand Up @@ -29,7 +29,7 @@
<script lang="ts">
import { computed, defineComponent, getCurrentInstance, toRefs, h } from 'vue';
import { ChevronRightIcon } from 'tdesign-icons-vue-next';
import { renderTNode, renderContent, TNode, useEmitEvent } from '../shared';
import { renderTNode, renderContent, TNode, useEmitEvent, Hover } from '../shared';
import config from '../config';
import CellProps from './props';
import { useFormDisabled } from '../form/hooks';
Expand All @@ -40,6 +40,7 @@ const name = `${prefix}-cell`;
export default defineComponent({
name,
components: { TNode },
directives: { Hover },
props: CellProps,
emits: ['click'],
setup(props, context) {
Expand Down
12 changes: 6 additions & 6 deletions src/cell/demos/single.vue
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<template>
<t-cell-group bordered>
<t-cell title="单行标题" arrow />
<t-cell title="单行标题" arrow required />
<t-cell title="单行标题" arrow>
<t-cell title="单行标题" arrow hover />
<t-cell title="单行标题" arrow hover required />
<t-cell title="单行标题" arrow hover>
<template #note>
<t-badge :count="16" />
</template>
</t-cell>
<t-cell title="单行标题">
<t-cell title="单行标题" hover>
<template #note>
<t-switch :default-value="true" />
</template>
</t-cell>
<t-cell title="单行标题" note="辅助信息" arrow />
<t-cell title="单行标题" :left-icon="lockIcon" arrow />
<t-cell title="单行标题" note="辅助信息" arrow hover />
<t-cell title="单行标题" :left-icon="lockIcon" arrow hover />
</t-cell-group>
</template>

Expand Down
38 changes: 38 additions & 0 deletions src/shared/hover.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { DirectiveBinding } from 'vue';

interface BindingObj {
active: boolean;
className: string;
}

const Hover = {
created(el: HTMLElement, binding: DirectiveBinding<BindingObj>) {
const startTime = 50;
const stayTime = 70;
const { className, active } = binding.value;

if (!active) return;

el.addEventListener(
'touchstart',
() => {
setTimeout(() => {
el?.classList.add(className);
}, startTime);
},
false,
);

el.addEventListener(
'touchend',
() => {
setTimeout(() => {
el?.classList.remove(className);
}, stayTime);
},
false,
);
},
};

export default Hover;
3 changes: 3 additions & 0 deletions src/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ export * from './useTouch';
export * from './useScrollParent';
export * from './useExpose';
export * from './useTest';

/* directives */
export { default as Hover } from './hover';

0 comments on commit fcd86b5

Please sign in to comment.