-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(HeightAnimation): add new component
- Loading branch information
1 parent
99629c4
commit ca2d911
Showing
27 changed files
with
340 additions
and
158 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
packages/dnb-design-system-portal/src/docs/uilib/components/height-animation.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
title: 'HeightAnimation' | ||
description: 'HeightAnimation is a helper component to animate from 0 to height:auto powered by CSS.' | ||
status: 'new' | ||
showTabs: true | ||
--- | ||
|
||
import HeightAnimationInfo from 'Docs/uilib/components/height-animation/info' | ||
import HeightAnimationDemos from 'Docs/uilib/components/height-animation/demos' | ||
|
||
<HeightAnimationInfo /> | ||
<HeightAnimationDemos /> |
60 changes: 60 additions & 0 deletions
60
packages/dnb-design-system-portal/src/docs/uilib/components/height-animation/Examples.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* UI lib Component Example | ||
* | ||
*/ | ||
|
||
import ComponentBox from 'dnb-design-system-portal/src/shared/tags/ComponentBox' | ||
|
||
export function HeightAnimationDefault() { | ||
return ( | ||
<ComponentBox useRender> | ||
{ | ||
/* jsx */ ` | ||
const Example = () => { | ||
const [openState, setOpenState] = React.useState(false) | ||
const onChangeHandler = ({ checked }) => { | ||
setOpenState(checked) | ||
} | ||
return ( | ||
<> | ||
<ToggleButton checked={openState} onChange={onChangeHandler}> | ||
Toggle me | ||
</ToggleButton> | ||
<StyledSection style_type="lavender"> | ||
<HeightAnimation | ||
top | ||
open={openState} | ||
element="div" // Optional | ||
animate={true} // Optional | ||
> | ||
<P className="content-element" space={0}> | ||
Your content | ||
</P> | ||
</HeightAnimation> | ||
</StyledSection> | ||
</> | ||
) | ||
} | ||
const StyledSection = styled(Section)\` | ||
.content-element { | ||
transition: transform 400ms var(--easing-default); | ||
transform: translateY(-2rem); | ||
padding: 4rem 0; | ||
} | ||
.dnb-height-animation--parallax .content-element { | ||
transform: translateY(0); | ||
} | ||
\` | ||
render(<Example />) | ||
` | ||
} | ||
</ComponentBox> | ||
) | ||
} |
13 changes: 13 additions & 0 deletions
13
...es/dnb-design-system-portal/src/docs/uilib/components/height-animation/demos.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
showTabs: true | ||
--- | ||
|
||
import { | ||
HeightAnimationDefault, | ||
} from 'Docs/uilib/components/height-animation/Examples' | ||
|
||
## Demos | ||
|
||
### HeightAnimation | ||
|
||
<HeightAnimationDefault /> |
7 changes: 7 additions & 0 deletions
7
...s/dnb-design-system-portal/src/docs/uilib/components/height-animation/events.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
showTabs: true | ||
--- | ||
|
||
## Events | ||
|
||
No events are supported at the moment. |
17 changes: 17 additions & 0 deletions
17
...ges/dnb-design-system-portal/src/docs/uilib/components/height-animation/info.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
showTabs: true | ||
--- | ||
|
||
## Description | ||
|
||
The HeightAnimation component is a helper component to animate from 0px to height:auto powered by CSS. It calculates the height on the fly. | ||
|
||
When the animation is done, it sets the element's height to `auto`. | ||
|
||
The component can be used as an opt-int replacement instead of vanilla HTML Elements. | ||
|
||
The element animation is done with a CSS transition and a `400ms` duration: | ||
|
||
## Accessibility | ||
|
||
It is important to never animate from 0 to e.g. 64px – because the content may differ based on the viewport width (screen size), the content itself, or the user may even have a larger `font-size`. |
12 changes: 12 additions & 0 deletions
12
...b-design-system-portal/src/docs/uilib/components/height-animation/properties.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
showTabs: true | ||
--- | ||
|
||
## Properties | ||
|
||
| Properties | Description | | ||
| ------------------------------------------- | ---------------------------------------------------------------------------------------------- | | ||
| `open` | _(optional)_ Set to `true` when the view should animate from 0px to auto. Defaults to `false`. | | ||
| `animate` | _(optional)_ Set to `false` to omit the animation. Defaults to `true`. | | ||
| `element` | _(optional)_ Custom HTML element for the component. Defaults to `div` HTML Element. | | ||
| [Space](/uilib/components/space/properties) | _(optional)_ spacing properties like `top` or `bottom` are supported. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 1 addition & 26 deletions
27
packages/dnb-design-system-portal/src/docs/uilib/helpers/hooks.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,4 @@ | ||
--- | ||
showTabs: true | ||
draft: true | ||
--- | ||
|
||
import { | ||
HeightAnimationExample, | ||
} from 'Docs/uilib/helpers/Examples' | ||
import SkipLinkExample from 'Docs/uilib/usage/accessibility/examples/skip-link-example.js' | ||
|
||
## Description | ||
|
||
These React Hooks are internally used in the components, and are with that a good choice when it comes to save bandwidth in the final production bundle. | ||
|
||
## `useHeightAnimation` | ||
|
||
In many places we want to animate the content in and out. The challenge is to never define a fixed height, because of an unknown content size and users' different font sizes. | ||
|
||
The `useHeightAnimation` hook takes an HTML Element, and animates it from 0 to the current content. When the animation is done, it sets the element's height to `auto`. | ||
|
||
The element animation is done with a CSS transition, e.g.: | ||
|
||
```css | ||
.animation-element { | ||
overflow: hidden; | ||
transition: height 1s var(--easing-default); | ||
} | ||
``` | ||
|
||
<HeightAnimationExample /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* ATTENTION: This file is auto generated by using "prepareTemplates". | ||
* Do not change the content! | ||
* | ||
*/ | ||
|
||
/** | ||
* Library Index height-animation to autogenerate all the components and extensions | ||
* Used by "prepareHeightAnimations" | ||
*/ | ||
|
||
import HeightAnimation from './height-animation/HeightAnimation' | ||
export * from './height-animation/HeightAnimation' | ||
export default HeightAnimation |
51 changes: 51 additions & 0 deletions
51
packages/dnb-eufemia/src/components/height-animation/HeightAnimation.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React from 'react' | ||
import classnames from 'classnames' | ||
import { DynamicElement } from '../../shared/interfaces' | ||
import { useHeightAnimation } from './useHeightAnimation' | ||
import Space from '../space/Space' | ||
|
||
export type HeightAnimationProps = { | ||
open: boolean | ||
animate?: boolean | ||
element?: DynamicElement | ||
className?: React.ReactNode | ||
children?: React.ReactNode | HTMLElement | ||
} | ||
|
||
export default function HeightAnimation({ | ||
open = false, | ||
animate = true, | ||
element, | ||
className, | ||
children, | ||
...props | ||
}: HeightAnimationProps) { | ||
const innerRef = React.useRef() | ||
const { isInDOM, isVisible, isVisibleParallax } = useHeightAnimation( | ||
innerRef, | ||
{ | ||
open, | ||
animate, | ||
} | ||
) | ||
|
||
if (!isInDOM) { | ||
return null | ||
} | ||
|
||
return ( | ||
<Space | ||
innerRef={innerRef} | ||
element={element || 'div'} | ||
className={classnames( | ||
'dnb-height-animation', | ||
isVisible && 'dnb-height-animation--is-visible', | ||
isVisibleParallax && 'dnb-height-animation--parallax', | ||
className | ||
)} | ||
{...props} | ||
> | ||
{children} | ||
</Space> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
packages/dnb-eufemia/src/components/height-animation/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* Component Entry | ||
* | ||
*/ | ||
|
||
import HeightAnimation from './HeightAnimation' | ||
export default HeightAnimation | ||
export * from './HeightAnimation' |
Oops, something went wrong.