Skip to content

Commit

Permalink
refactor(animate): move to script setup (#2947)
Browse files Browse the repository at this point in the history
  • Loading branch information
eiinu authored Mar 6, 2024
1 parent 87964fa commit b80f3fb
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 170 deletions.
1 change: 1 addition & 0 deletions src/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,7 @@
"show": true,
"taro": true,
"tarodoc": false,
"setup": true,
"type": "component",
"author": "lijiamiao"
},
Expand Down
4 changes: 2 additions & 2 deletions src/packages/__VUE/animate/__tests__/animate.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { mount } from '@vue/test-utils';
import Animate from '../index.vue';
import Animate from '../';
import { nextTick } from 'vue';
import { AnimateType } from '../type';
import { AnimateType } from '../types';
import { sleep } from '@/packages/utils/unit';

const testType = [
Expand Down
75 changes: 75 additions & 0 deletions src/packages/__VUE/animate/animate.taro.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<template>
<view class="nut-animate">
<view
:class="classes"
:style="{
animationDuration: duration ? `${duration}ms` : undefined
}"
@click="handleClick"
>
<slot></slot>
</view>
</view>
</template>
<script setup lang="ts">
import { ref, computed, watch } from 'vue';
import type { AnimateType, AnimateAction } from './types';
defineOptions({
name: 'NutAnimate'
});
export type AnimateProps = Partial<{
type: AnimateType;
show: boolean;
action: AnimateAction;
loop: boolean;
duration: string | number;
}>;
const props = withDefaults(defineProps<AnimateProps>(), {
show: false,
action: '',
loop: false,
duration: 500
});
const emit = defineEmits(['click', 'animate']);
const animated = ref(props.action === 'initial' || props.show === true || props.loop);
const classes = computed(() => {
const prefixCls = 'nut-animate';
return {
'nut-animate__container': true,
[`${prefixCls}-${props.type}`]: animated.value,
loop: props.loop
};
});
const animate = () => {
animated.value = false;
requestAnimationFrame(() => {
requestAnimationFrame(() => {
animated.value = true;
});
});
};
const handleClick = (event: Event) => {
if (props.action === 'click') {
animate();
emit('click', event);
emit('animate');
}
};
watch(
() => props.show,
(val) => {
if (val) {
animate();
emit('animate');
}
}
);
</script>
75 changes: 75 additions & 0 deletions src/packages/__VUE/animate/animate.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<template>
<view class="nut-animate">
<view
:class="classes"
:style="{
animationDuration: duration ? `${duration}ms` : undefined
}"
@click="handleClick"
>
<slot></slot>
</view>
</view>
</template>
<script setup lang="ts">
import { ref, computed, watch } from 'vue';
import type { AnimateType, AnimateAction } from './types';
defineOptions({
name: 'NutAnimate'
});
export type AnimateProps = Partial<{
type: AnimateType;
show: boolean;
action: AnimateAction;
loop: boolean;
duration: string | number;
}>;
const props = withDefaults(defineProps<AnimateProps>(), {
show: false,
action: '',
loop: false,
duration: 500
});
const emit = defineEmits(['click', 'animate']);
const animated = ref(props.action === 'initial' || props.show === true || props.loop);
const classes = computed(() => {
const prefixCls = 'nut-animate';
return {
'nut-animate__container': true,
[`${prefixCls}-${props.type}`]: animated.value,
loop: props.loop
};
});
const animate = () => {
animated.value = false;
requestAnimationFrame(() => {
requestAnimationFrame(() => {
animated.value = true;
});
});
};
const handleClick = (event: Event) => {
if (props.action === 'click') {
animate();
emit('click', event);
emit('animate');
}
};
watch(
() => props.show,
(val) => {
if (val) {
animate();
emit('animate');
}
}
);
</script>
13 changes: 13 additions & 0 deletions src/packages/__VUE/animate/doc.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,16 @@ app.use(Animate);
| 9 | jump | jump,It is recommended that loop be true |
| 10 | twinkle | twinkle,It is recommended that loop be true |
| 11 | flicker | Polish button,It is recommended that loop be true |

### Types version

The component exports the following type definitions:

```js
import type {
AnimateType,
AnimateAction,
AnimateProps,
AnimateInstance
} from '@nutui/nutui';
```
13 changes: 13 additions & 0 deletions src/packages/__VUE/animate/doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,16 @@ app.use(Animate);
| 9 | jump | 跳跃,建议 loop 为 true |
| 10 | twinkle | 水波,建议 loop 为 true |
| 11 | flicker | 擦亮按钮,建议 loop 为 true |

### 类型定义 version

组件导出以下类型定义:

```js
import type {
AnimateType,
AnimateAction,
AnimateProps,
AnimateInstance
} from '@nutui/nutui';
```
13 changes: 13 additions & 0 deletions src/packages/__VUE/animate/doc.taro.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,16 @@ app.use(Animate);
| 9 | jump | 跳跃,建议 loop 为 true |
| 10 | twinkle | 水波,建议 loop 为 true |
| 11 | flicker | 擦亮按钮,建议 loop 为 true |

### 类型定义 version

组件导出以下类型定义:

```js
import type {
AnimateType,
AnimateAction,
AnimateProps,
AnimateInstance
} from '@nutui/nutui-taro';
```
13 changes: 13 additions & 0 deletions src/packages/__VUE/animate/index.taro.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Animate from './animate.taro.vue';
import type { ComponentPublicInstance } from 'vue';
import { withInstall } from '@/packages/utils';

withInstall(Animate);

export type { AnimateProps } from './animate.taro.vue';

export type { AnimateType, AnimateAction } from './types';

export type AnimateInstance = ComponentPublicInstance & InstanceType<typeof Animate>;

export { Animate, Animate as default };
84 changes: 0 additions & 84 deletions src/packages/__VUE/animate/index.taro.vue

This file was deleted.

13 changes: 13 additions & 0 deletions src/packages/__VUE/animate/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Animate from './animate.vue';
import type { ComponentPublicInstance } from 'vue';
import { withInstall } from '@/packages/utils';

withInstall(Animate);

export type { AnimateProps } from './animate.vue';

export type { AnimateType, AnimateAction } from './types';

export type AnimateInstance = ComponentPublicInstance & InstanceType<typeof Animate>;

export { Animate, Animate as default };
Loading

0 comments on commit b80f3fb

Please sign in to comment.