Skip to content

feat(helper): add transition helpers with components and styles #413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions docs/tools/helper/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,73 @@
```

:::

## Component

### FadeInExpandTransition

Provides fade-in transition effects when block-level elements expand, supporting both `height` or `width` properties.

**Props:**

```ts
interface FadeInExpandTransitionProps {
/**
* Whether to group transitions
*/
group?: boolean
/**
* Transition mode
*/
mode?: 'default' | 'in-out' | 'out-in'

/**
* Whether to switch to the transition of `width`
*
* @default false
*/
width?: boolean

appear?: boolean
onLeave?: () => void
onAfterEnter?: () => void
onAfterLeave?: () => void
}
```

**Import Styles:**

Transition animations require importing the following CSS files as needed:

- `@vuepress/helper/transition/fade-in-height-expand.css` - `height` transition animation

- `@vuepress/helper/transition/fade-in-width-expand.css` - `width` transition animation

::: tip Only one CSS file needs to be imported

:::

**Usage:**

```vue
<script setup lang="ts">
import { FadeInExpandTransition } from '@vuepress/helper/client'

import '@vuepress/helper/transition/fade-in-height-expand.css'
// import '@vuepress/helper/transition/fade-in-width-expand.css'

const expand = ref(false)
</script>

<template>
<button @click="expand = !expand">

Check failure on line 225 in docs/tools/helper/client.md

View workflow job for this annotation

GitHub Actions / build-check

Missing an explicit type attribute for button

Check failure on line 225 in docs/tools/helper/client.md

View workflow job for this annotation

GitHub Actions / bundle-check

Missing an explicit type attribute for button

Check failure on line 225 in docs/tools/helper/client.md

View workflow job for this annotation

GitHub Actions / build-check

Missing an explicit type attribute for button

Check failure on line 225 in docs/tools/helper/client.md

View workflow job for this annotation

GitHub Actions / bundle-check

Missing an explicit type attribute for button
{{ expand ? 'Collapse' : 'Expand' }}
</button>

<FadeInExpandTransition>
<div v-show="expand">
<p>Content</p>
</div>
</FadeInExpandTransition>
</template>
```
61 changes: 61 additions & 0 deletions docs/tools/helper/style.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,64 @@
## Normalize

`@vuepress/helper/normalize.css` is a CSS file that normalizes the default styles of the browser. It is recommended to import it in community themes.

## Transitions

`@vuepress/helper/transition/*.css` is a collection of CSS files that provide transitions for elements. It is recommended to import for use as needed in community themes.

- `fade-in.css`
- `fade-in-up.css`
- `fade-in-down.css`
- `fade-in-left.css`
- `fade-in-right.css`
- `fade-in-scale-up.css`
- `slide-in-up.css`
- `slide-in-down.css`
- `slide-in-left.css`
- `slide-in-right.css`

**Usage:**

```vue
<script setup>
import '@vuepress/helper/transition/fade-in.css'
</script>

<template>
<Transition name="fade-in">
<div>...</div>

Check failure on line 37 in docs/tools/helper/style.md

View workflow job for this annotation

GitHub Actions / build-check

The element inside `<transition>` is expected to have a `v-if` or `v-show` directive

Check failure on line 37 in docs/tools/helper/style.md

View workflow job for this annotation

GitHub Actions / bundle-check

The element inside `<transition>` is expected to have a `v-if` or `v-show` directive

Check failure on line 37 in docs/tools/helper/style.md

View workflow job for this annotation

GitHub Actions / build-check

The element inside `<transition>` is expected to have a `v-if` or `v-show` directive

Check failure on line 37 in docs/tools/helper/style.md

View workflow job for this annotation

GitHub Actions / bundle-check

The element inside `<transition>` is expected to have a `v-if` or `v-show` directive
</Transition>
</template>
```

**CSS Variables:**

```css
:root {
/* general transitions variables */

--transition-ease-in-out: cubic-bezier(0.4, 0, 0.2, 1);
--transition-ease-out: cubic-bezier(0, 0, 0.2, 1);
--transition-ease-in: cubic-bezier(0.4, 0, 1, 1);
--transition-duration: 0.3s;
--transition-enter-duration: var(--transition-duration);
--transition-leave-duration: 0.2s;
--transition-delay: 0.1s;

/* specific transitions variables */

--transition-fade-in-up-offset: 10px;
--transition-fade-in-down-offset: -10px;
--transition-fade-in-left-offset: 10px;
--transition-fade-in-right-offset: -10px;

--transition-fade-in-scale-up-scale: 0.9;
--transition-fade-in-scale-up-duration: 0.2s;
--transition-fade-in-scale-up-origin: inherit;

--transition-slide-in-up-offset: 100%;
--transition-slide-in-down-offset: -100%;
--transition-slide-in-left-offset: 100%;
--transition-slide-in-right-offset: -100%;
}
```
68 changes: 68 additions & 0 deletions docs/zh/tools/helper/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,71 @@
```

:::

## 组件{#component}

### FadeInExpandTransition

为块级元素的展开提供淡入淡出过渡效果,支持 `height` 或 `width` 属性。

**Props:**

```ts
interface FadeInExpandTransitionProps {
/**
* 是否分组过渡
*/
group?: boolean
/**
* 过渡模式
*/
mode?: 'default' | 'in-out' | 'out-in'

/**
* 是否切换为 `width` 的过渡
*
* @default false
*/
width?: boolean

appear?: boolean
onLeave?: () => void
onAfterEnter?: () => void
onAfterLeave?: () => void
}
```

**导入样式:**

过渡动画需要按需引入以下 CSS 文件:

- `@vuepress/helper/transition/fade-in-height-expand.css` - `height` 过渡动画
- `@vuepress/helper/transition/fade-in-width-expand.css` - `width` 过渡动画

::: tip 只需要引入其中一个 CSS 文件
:::

**Usage:**

```vue
<script setup lang="ts">
import { FadeInExpandTransition } from '@vuepress/helper/client'

import '@vuepress/helper/transition/fade-in-height-expand.css'
// import '@vuepress/helper/transition/fade-in-width-expand.css'

const expand = ref(false)
</script>

<template>
<button @click="expand = !expand">

Check failure on line 223 in docs/zh/tools/helper/client.md

View workflow job for this annotation

GitHub Actions / build-check

Missing an explicit type attribute for button

Check failure on line 223 in docs/zh/tools/helper/client.md

View workflow job for this annotation

GitHub Actions / bundle-check

Missing an explicit type attribute for button

Check failure on line 223 in docs/zh/tools/helper/client.md

View workflow job for this annotation

GitHub Actions / build-check

Missing an explicit type attribute for button

Check failure on line 223 in docs/zh/tools/helper/client.md

View workflow job for this annotation

GitHub Actions / bundle-check

Missing an explicit type attribute for button
{{ expand ? 'Collapse' : 'Expand' }}
</button>

<FadeInExpandTransition>
<div v-show="expand">
<p>Content</p>
</div>
</FadeInExpandTransition>
</template>
```
61 changes: 61 additions & 0 deletions docs/zh/tools/helper/style.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,64 @@
## 规范化

`@vuepress/helper/normalize.css` 是一个 CSS 文件,用于规范化浏览器的默认样式。推荐在社区主题中引入它。

## 过渡{#transitions}

`@vuepress/helper/transition/*.css` 是一组提供元素过渡效果的CSS文件集合,推荐在社区主题中按需导入使用。

- `fade-in.css`
- `fade-in-up.css`
- `fade-in-down.css`
- `fade-in-left.css`
- `fade-in-right.css`
- `fade-in-scale-up.css`
- `slide-in-up.css`
- `slide-in-down.css`
- `slide-in-left.css`
- `slide-in-right.css`

**使用:**

```vue
<script setup>
import '@vuepress/helper/transition/fade-in.css'
</script>

<template>
<Transition name="fade-in">
<div>...</div>

Check failure on line 37 in docs/zh/tools/helper/style.md

View workflow job for this annotation

GitHub Actions / build-check

The element inside `<transition>` is expected to have a `v-if` or `v-show` directive

Check failure on line 37 in docs/zh/tools/helper/style.md

View workflow job for this annotation

GitHub Actions / bundle-check

The element inside `<transition>` is expected to have a `v-if` or `v-show` directive

Check failure on line 37 in docs/zh/tools/helper/style.md

View workflow job for this annotation

GitHub Actions / build-check

The element inside `<transition>` is expected to have a `v-if` or `v-show` directive

Check failure on line 37 in docs/zh/tools/helper/style.md

View workflow job for this annotation

GitHub Actions / bundle-check

The element inside `<transition>` is expected to have a `v-if` or `v-show` directive
</Transition>
</template>
```

**CSS 变量:**

```css
:root {
/* general transitions variables */

--transition-ease-in-out: cubic-bezier(0.4, 0, 0.2, 1);
--transition-ease-out: cubic-bezier(0, 0, 0.2, 1);
--transition-ease-in: cubic-bezier(0.4, 0, 1, 1);
--transition-duration: 0.3s;
--transition-enter-duration: var(--transition-duration);
--transition-leave-duration: 0.2s;
--transition-delay: 0.1s;

/* specific transitions variables */

--transition-fade-in-up-offset: 10px;
--transition-fade-in-down-offset: -10px;
--transition-fade-in-left-offset: 10px;
--transition-fade-in-right-offset: -10px;

--transition-fade-in-scale-up-scale: 0.9;
--transition-fade-in-scale-up-duration: 0.2s;
--transition-fade-in-scale-up-origin: inherit;

--transition-slide-in-up-offset: 100%;
--transition-slide-in-down-offset: -100%;
--transition-slide-in-left-offset: 100%;
--transition-slide-in-right-offset: -100%;
}
```
1 change: 1 addition & 0 deletions tools/helper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"./colors.css": "./lib/client/styles/colors.css",
"./normalize.css": "./lib/client/styles/normalize.css",
"./sr-only.css": "./lib/client/styles/sr-only.css",
"./transition/*.css": "./lib/client/styles/transition/*.css",
"./scss/*.scss": {
"sass": "./scss/*.scss"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import type { Component, PropType } from 'vue'
import { Transition, TransitionGroup, defineComponent, h } from 'vue'

export const FadeInExpandTransition = defineComponent({
name: 'FadeInExpandTransition',
props: {
/* Whether to group transitions */
group: Boolean,
appear: Boolean,
/* Whether to switch to the transition of `width` */
width: Boolean,
mode: String as PropType<'default' | 'in-out' | 'out-in'>,
onLeave: Function,
onAfterLeave: Function,
onAfterEnter: Function,
},
setup(props, { slots }) {
const handleBeforeLeave = (el: HTMLElement): void => {
if (props.width) {
el.style.maxWidth = `${el.offsetWidth}px`
} else {
el.style.maxHeight = `${el.offsetHeight}px`
}
void el.offsetWidth
}

const handleLeave = (el: HTMLElement): void => {
if (props.width) {
el.style.maxWidth = '0'
} else {
el.style.maxHeight = '0'
}
void el.offsetWidth
props.onLeave?.()
}

const handleAfterLeave = (el: HTMLElement): void => {
if (props.width) {
el.style.maxWidth = ''
} else {
el.style.maxHeight = ''
}
props.onAfterLeave?.()
}

const handleEnter = (el: HTMLElement): void => {
el.style.transition = 'none'
if (props.width) {
const memorizedWidth = el.offsetWidth
el.style.maxWidth = '0'
void el.offsetWidth
el.style.transition = ''
el.style.maxWidth = `${memorizedWidth}px`
} else {
const memorizedHeight = el.offsetHeight
el.style.maxHeight = '0'
void el.offsetWidth
el.style.transition = ''
el.style.maxHeight = `${memorizedHeight}px`
}
void el.offsetWidth
}

const handleAfterEnter = (el: HTMLElement): void => {
if (props.width) {
el.style.maxWidth = ''
} else {
el.style.maxHeight = ''
}
props.onAfterEnter?.()
}

return () =>
h(
(props.group ? TransitionGroup : Transition) as Component,
{
name: props.width ? 'fade-in-width-expand' : 'fade-in-height-expand',
appear: props.appear,
mode: props.mode,
onEnter: handleEnter,
onAfterEnter: handleAfterEnter,
onBeforeLeave: handleBeforeLeave,
onLeave: handleLeave,
onAfterLeave: handleAfterLeave,
},
slots,
)
},
})
1 change: 1 addition & 0 deletions tools/helper/src/client/components/Transitions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './FadeInExpandTransition.js'
1 change: 1 addition & 0 deletions tools/helper/src/client/components/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './LoadingIcon.js'
export * from './Transitions/index.js'
Loading
Loading