Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 10 additions & 4 deletions src/shared/button/Button.modules.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
border: none;
border-radius: 5px;
cursor: pointer;
}

.disabled {
cursor: not-allowed;
opacity: 0.6;
&:hover {
&:not([disabled]) {
background-color: #0056b3;
}
}

&:disabled {
cursor: not-allowed;
opacity: 0.6;
}
}
3 changes: 1 addition & 2 deletions src/shared/button/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { ReactNode, MouseEvent } from 'react';
import cn from 'clsx';
import s from './Button.modules.scss';

type ButtonProps = {
Expand All @@ -9,7 +8,7 @@ type ButtonProps = {
};

export const Button = ({ children, onClick, disabled = false }: ButtonProps) => (
<button className={cn(s.button, { [s.disabled]: disabled })} onClick={onClick}>
<button className={s.button} onClick={onClick} disabled={disabled}>
{children}
</button>
);
27 changes: 27 additions & 0 deletions src/shared/collapse/Collapse.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.collapse-container {
border: 1px solid #ccc;
border-radius: 4px;
margin: 10px 0;
overflow: hidden;
}

.collapse-button {
width: 100%;
background-color: #007bff;
color: white;
padding: 10px;
border: none;
cursor: pointer;
text-align: left;
font-size: 1em;

&:hover {
background-color: #0056b3;
}
}

.collapse-content {
padding: 10px;
background-color: #f9f9f9;
transition: height 0.3s ease;
}
50 changes: 50 additions & 0 deletions src/shared/collapse/Collapse.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React, { useState, useRef, useEffect, ReactNode } from 'react';
import s from './Collapse.module.scss';

type CollapseProps = {
title: string;
children: ReactNode;
};

export const Collapse = ({ title, children }: CollapseProps) => {
const [isOpen, setIsOpen] = useState(false);
const [height, setHeight] = useState(0);
const contentRef = useRef<HTMLDivElement>(null);

const toggleCollapse = () => {
setIsOpen(!isOpen);
};

useEffect(() => {
const resizeObserver = new ResizeObserver((entries) =>
entries.forEach((entry) => setHeight(entry.borderBoxSize[0].blockSize))
);

const currentContentRef = contentRef.current;

currentContentRef && resizeObserver.observe(currentContentRef);

return () => {
currentContentRef && resizeObserver.unobserve(currentContentRef);
};
}, []);

return (
<div className={s['collapse-container']}>
<button className={s['collapse-button']} onClick={toggleCollapse}>
{title}
</button>
<div
style={{
height: `${isOpen ? height : 0}px`,
overflow: 'hidden',
transition: 'height 300ms ease',
}}
>
<div ref={contentRef} className={s['collapse-content']}>
{children}
</div>
</div>
</div>
);
};
29 changes: 29 additions & 0 deletions src/stories/Collapse.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React from 'react';
import type { Meta } from '@storybook/react';

import { Collapse } from '../shared/collapse/Collapse';
import { Button } from '../shared/button/Button';

const meta: Meta<typeof Collapse> = {
component: Collapse,
title: 'Сложные компоненты/Сворачивание',
tags: ['autodocs'],
};

export default meta;

export const Test = {
args: {
title: 'Сворачиваемый контент',
children: (
<>
<Button>Some button</Button>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Incidunt, perferendis! Non autem consectetur amet
sint at saepe veniam doloremque, culpa dolore corporis ad consequuntur temporibus? Magni laudantium eum dolor
tempora.
</p>
</>
),
},
};