-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #292 from TobiTRy/buildIn--Autosizing-Box
Build in autosizing box
- Loading branch information
Showing
37 changed files
with
352 additions
and
174 deletions.
There are no files selected for viewing
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
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
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
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
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 @@ | ||
import { styled } from 'styled-components'; | ||
|
||
export const InnerContentWrapper = styled.div` | ||
transition: | ||
height 0.3s ease-in-out, | ||
width 0.3s ease-in-out; | ||
overflow: hidden; | ||
`; |
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,44 @@ | ||
import React, { useState, useRef, useEffect } from 'react'; | ||
import { InnerContentWrapper } from './AutoSizingBox.style'; | ||
import { TAutoSizingBox } from './TAutoSizingBox.model'; | ||
|
||
// --------------------------------------------------------------------------- // | ||
// A Box thats adjusts width and height dynamicly from the childs via animation// | ||
// --------------------------------------------------------------------------- // | ||
export default function AutoSizingBox(props: TAutoSizingBox) { | ||
const { children, startHeight, startWidth, adjustHeight = true, adjustWidth } = props; | ||
|
||
// State for the height and width of the box | ||
const [boxHeight, setBoxHeight] = useState(startHeight || 'auto'); | ||
const [boxWidth, setBoxWidth] = useState(startWidth || 'auto'); | ||
|
||
// Reference to the box element to get the scrollHeight and scrollWidth | ||
const boxRef = useRef<HTMLDivElement>(null); | ||
|
||
useEffect(() => { | ||
// ResizeObserver to observe the box element | ||
const resizeObserver = new ResizeObserver(() => { | ||
if (boxRef.current) { | ||
if (adjustHeight) { | ||
setBoxHeight(boxRef.current.scrollHeight); | ||
} | ||
if (adjustWidth) { | ||
setBoxWidth(boxRef.current.scrollWidth); | ||
} | ||
} | ||
}); | ||
|
||
if (boxRef.current) { | ||
// Start observing the box element for changes | ||
resizeObserver.observe(boxRef.current); | ||
} | ||
// Cleanup the observer on unmount or before re-running this effect | ||
return () => resizeObserver.disconnect(); | ||
}, [children, adjustHeight, adjustWidth]); | ||
|
||
return ( | ||
<InnerContentWrapper style={{ height: boxHeight, width: boxWidth }}> | ||
<div ref={boxRef}>{children}</div> | ||
</InnerContentWrapper> | ||
); | ||
} |
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,9 @@ | ||
export type TAutoSizingBox = { | ||
children?: React.ReactNode; | ||
startHeight?: number; | ||
startWidth?: number; | ||
adjustHeight?: boolean; | ||
adjustWidth?: boolean; | ||
}; | ||
|
||
export type TAutoSizingBoxWithHTMLAttrs = TAutoSizingBox & React.HTMLAttributes<HTMLDivElement>; |
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,40 @@ | ||
import { Meta, Controls, Title, Description, Canvas } from '@storybook/blocks'; | ||
|
||
import * as AutoSizingBox from './AutoSizingBox.stories.tsx'; | ||
|
||
<Meta of={AutoSizingBox} /> | ||
|
||
<Title /> | ||
|
||
### Brief Description | ||
|
||
<Description /> | ||
|
||
### Setup Instructions | ||
|
||
1. Import the `AutoSizingBox` component where you need to use it: | ||
|
||
```javascript | ||
import { AutoSizingBox } from 'fui-fancyui'; | ||
``` | ||
|
||
2. Make sure to import the `TAutoSizingBox` type definitions if you are using TypeScript: | ||
```javascript | ||
import { TAutoSizingBox } from 'fui-fancyui'; | ||
``` | ||
|
||
### Example Usage | ||
|
||
Here's how to use the `AutoSizingBox` component in a React component: | ||
|
||
```jsx | ||
<AutoSizingBox startHeight="100px" startWidth="100px" adjustHeight={true} adjustWidth={true}> | ||
<div>Dynamic Content Here</div> | ||
</AutoSizingBox> | ||
``` | ||
|
||
<Canvas /> | ||
|
||
### Component Properties | ||
|
||
<Controls /> |
106 changes: 106 additions & 0 deletions
106
src/components/atoms/AutoSizingBox/docu/AutoSizingBox.stories.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,106 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import AutoSizingBox from '../AutoSizingBox'; | ||
import { TAutoSizingBox } from '../TAutoSizingBox.model'; | ||
import React from 'react'; | ||
import { FancyButton } from '@/components/organisms/FancyButton'; | ||
import { FancyBox } from '@/components/atoms/FancyBox'; | ||
|
||
// More on how to set up stories at: https://storybook.js.org/docs/react/writing-stories/introduction#default-export | ||
const meta = { | ||
component: AutoSizingBox, | ||
title: 'components/atoms/AutoSizingBox', | ||
parameters: { | ||
// Optional parameter to center the component in the Canvas. More info: https://storybook.js.org/docs/react/configure/story-layout | ||
docs: { | ||
description: { | ||
component: | ||
'The `AutoSizingBox` is a React component designed to dynamically adjust its width and height based on the size of its child elements. It uses CSS transitions for smooth animations during these adjustments.', | ||
}, | ||
}, | ||
}, | ||
argTypes: { | ||
children: { | ||
description: 'This is the content of the box', | ||
control: { type: 'object' }, | ||
table: { | ||
category: 'Content', | ||
}, | ||
}, | ||
startHeight: { | ||
description: 'This is the initial height of the box', | ||
control: { type: 'number' }, | ||
}, | ||
startWidth: { | ||
description: 'This is the initial width of the box', | ||
control: { type: 'number' }, | ||
table: { | ||
defaultValue: { summary: 'number' }, | ||
}, | ||
}, | ||
adjustHeight: { | ||
description: 'This is the flag to adjust the height of the box', | ||
control: { type: 'boolean' }, | ||
table: { | ||
defaultValue: { summary: 'true' }, | ||
}, | ||
}, | ||
adjustWidth: { | ||
description: 'This is the flag to adjust the width of the box', | ||
control: { type: 'boolean' }, | ||
table: { | ||
defaultValue: { summary: 'false' }, | ||
}, | ||
}, | ||
}, | ||
} satisfies Meta<typeof AutoSizingBox>; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof meta>; | ||
|
||
const HelperComponent = (props: TAutoSizingBox) => { | ||
const [isopen, setIsOpen] = React.useState(false); | ||
|
||
return ( | ||
<FancyBox | ||
themeType="secondary" | ||
borderRadius={'sm'} | ||
externalStyle={{ | ||
color: 'black', | ||
}} | ||
> | ||
<AutoSizingBox {...props}> | ||
<FancyButton label={isopen ? 'close' : 'open'} onClick={() => setIsOpen((prev) => !prev)} /> | ||
{isopen ? <TestComps /> : ''} | ||
</AutoSizingBox> | ||
</FancyBox> | ||
); | ||
}; | ||
|
||
// More on writing stories with args: https://storybook.js.org/docs/react/writing-stories/args | ||
export const Primary: Story = { | ||
render: (args) => <HelperComponent {...args} />, | ||
args: { | ||
startHeight: 100, | ||
startWidth: 200, | ||
adjustHeight: true, | ||
adjustWidth: false, | ||
}, | ||
}; | ||
|
||
const TestComps = () => [ | ||
<> | ||
<div>Hiii</div> | ||
<div>Hiii</div> | ||
<div>Hiii</div> | ||
<div>Hiii</div> | ||
<div>Hiii</div> | ||
<div>Hiii</div> | ||
<div>Hiii</div> | ||
<div>Hiii</div> | ||
<div>Hiii</div> | ||
<div>Hiii</div> | ||
<div>Hiii</div> | ||
</>, | ||
]; |
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,3 @@ | ||
export { default as AutoSizingBox } from './AutoSizingBox'; | ||
|
||
export type { TAutoSizingBoxWithHTMLAttrs, TAutoSizingBox } from './TAutoSizingBox.model'; |
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
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
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
Oops, something went wrong.