Skip to content

Commit 63306cc

Browse files
author
chenyueban
committed
feat(Transition): add prop wrapper, and new type 'collapse'
1 parent 0400b7d commit 63306cc

4 files changed

Lines changed: 60 additions & 5 deletions

File tree

packages/fluent-ui.com/src/docs/components/Transition/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,22 @@ The Transition component's `mountOnEnter` property prevents the child component
7272
)
7373
}
7474
```
75+
76+
## Collapse
77+
78+
```jsx
79+
() => {
80+
const [visible, setVisible] = React.useState(false)
81+
function handleVisible() {
82+
setVisible(v => !v)
83+
}
84+
return (
85+
<>
86+
<Toggle checked={visible} onChange={handleVisible} />
87+
<Transition visible={visible} type="collapse">
88+
<Box width={50} height={50} margin={2} backgroundColor="white.default"></Box>
89+
</Transition>
90+
</>
91+
)
92+
}
93+
```

packages/fluent-ui.com/src/docs/components/Transition/api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import { Transition } from '@fluent-ui/core'
1818
| --- | --- | --- | --- |
1919
| children | React.ReactNode | | Elements that require transition effects. |
2020
| visible | boolean | false | Control element display. |
21-
| type | 'fade' &or; 'zoom' &or; 'slide' | 'fade' | Transition effects type. |
21+
| type | 'fade' &or; 'zoom' &or; 'slide' &or; 'collapse' | 'fade' | Transition effects type. |
22+
| wrapper | boolean | true | If `true` a layer of div will be nested outside the target to display the animation, otherwise the animation will be displayed directly on the target. |
2223
| mountOnEnter | boolean | false | By default the child component is mounted immediately along with the parent `Transition` component. If you want to "lazy mount" the component on the first `in={true}` you can set `mountOnEnter`. After the first enter transition the component will stay mounted, even on "exited", unless you also specify `unmountOnExit`. |
2324
| unmountOnExit | boolean | false | By default the child component stays mounted after it reaches the `'exited'` state. Set `unmountOnExit` if you'd prefer to unmount the component after it finishes exiting. |
2425
| appear | boolean | true | The component will transition in as soon as the `<Transition>` mounts. |

packages/fluent-ui/src/Transition/Transition.styled.ts renamed to packages/fluent-ui/src/Transition/Transition.styled.tsx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import * as React from 'react'
22
import styled, { css } from 'styled-components'
33
import { variant } from '@xstyled/system'
44
import { createBaseTransition } from '../styles/createTransition'
5+
import { Box } from '..'
56

6-
export type Type = 'fade' | 'zoom' | 'slide'
7+
export type Type = 'fade' | 'zoom' | 'slide' | 'collapse'
78
export interface StyledContainerProps {
89
type?: Type
910
visible?: boolean
11+
wrapper?: boolean
1012
}
1113

1214
export const fade = css`
@@ -60,6 +62,21 @@ export const slide = css`
6062
}
6163
`
6264

65+
export const collapse = css`
66+
&.collapse-enter {
67+
transform: scaleY(0);
68+
&-active {
69+
transform: scaleY(1);
70+
}
71+
}
72+
&.collapse-exit {
73+
transform: scaleY(1);
74+
&-active {
75+
transform: scaleY(0);
76+
}
77+
}
78+
`
79+
6380
export const type = variant({
6481
prop: 'type',
6582
default: 'fade',
@@ -78,16 +95,25 @@ export const type = variant({
7895
transition: ${createBaseTransition(['transform'])};
7996
${({ visible }: StyledContainerProps): string | false =>
8097
visible ? `transform: none;` : `transform: translateY(100vh);`}
98+
`,
99+
collapse: css`
100+
overflow: hidden;
101+
transform-origin: top left;
102+
transition: ${createBaseTransition(['transform'])};
103+
${({ visible }: StyledContainerProps): string | false =>
104+
visible ? `transform: scaleY(1);` : `transform: scaleY(0);`}
81105
`
82106
}
83107
})
84108

85109
export const StyledContainer = styled(
86-
({ children, ...props }): React.ReactElement => React.cloneElement(children, props)
110+
({ children, wrapper, ...props }): React.ReactElement =>
111+
wrapper ? <Box {...props}>{children}</Box> : React.cloneElement(children, props)
87112
)<StyledContainerProps>`
88113
${type}
89114
90115
${fade}
91116
${zoom}
92117
${slide}
118+
${collapse}
93119
`

packages/fluent-ui/src/Transition/Transition.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,25 @@ import { Type, StyledContainer } from './Transition.styled'
66
export interface TransitionProps extends Omit<CSSTransitionProps, 'in'> {
77
type?: Type
88
visible?: boolean
9+
wrapper?: boolean
910
}
1011

1112
const Transition = React.forwardRef<HTMLElement, TransitionProps>(
1213
(
13-
{ type = 'fade', appear = true, timeout = 250, visible, children, ...rest }: TransitionProps,
14+
{
15+
type = 'fade',
16+
wrapper = true,
17+
appear = true,
18+
timeout = 250,
19+
visible,
20+
children,
21+
...rest
22+
}: TransitionProps,
1423
ref
1524
): React.ReactElement => {
1625
return (
1726
<CSSTransition in={visible} appear={appear} timeout={timeout} classNames={type} {...rest}>
18-
<StyledContainer ref={ref} visible={visible} type={type}>
27+
<StyledContainer ref={ref} visible={visible} type={type} wrapper={wrapper}>
1928
{children}
2029
</StyledContainer>
2130
</CSSTransition>

0 commit comments

Comments
 (0)