Skip to content

Commit

Permalink
code refactoring [MDS-771]
Browse files Browse the repository at this point in the history
  • Loading branch information
yarema184 committed Nov 8, 2023
1 parent 712e376 commit fc99df8
Show file tree
Hide file tree
Showing 19 changed files with 124 additions and 100 deletions.
2 changes: 1 addition & 1 deletion docs/app/components/server/accordion/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ContentOutsideSizes } from '@/app/components/server/accordion/examples/
import { Default } from '@/app/components/server/accordion/examples/Default';
import { Disabled } from '@/app/components/server/accordion/examples/Disabled';
import { Sizes } from '@/app/components/server/accordion/examples/Sizes';
import ExampleSection from '@/app/components/shared/ExampleSection';
import ExampleSection from '@/app/components/shared/exampleSection/ExampleSection';
import QuickNav from '@/app/components/shared/QuickNav';
import { getExamples } from '@/app/utils/getExamples';
import { MDX } from '@/components/MDX';
Expand Down
2 changes: 1 addition & 1 deletion docs/app/components/server/button/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Icons } from '@/app/components/server/button/examples/Icons';
import { Multiline } from '@/app/components/server/button/examples/Multiline';
import { Sizes } from '@/app/components/server/button/examples/Sizes';
import { Variants } from '@/app/components/server/button/examples/Variants';
import ExampleSection from '@/app/components/shared/ExampleSection';
import ExampleSection from '@/app/components/shared/exampleSection/ExampleSection';
import QuickNav from '@/app/components/shared/QuickNav';
import { getExamples } from '@/app/utils/getExamples';
import { MDX } from '@/components/MDX';
Expand Down
2 changes: 1 addition & 1 deletion docs/app/components/server/checkbox/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Default } from '@/app/components/server/checkbox/examples/Default';
import ExampleSection from '@/app/components/shared/ExampleSection';
import ExampleSection from '@/app/components/shared/exampleSection/ExampleSection';
import QuickNav from '@/app/components/shared/QuickNav';
import { getExamples } from '@/app/utils/getExamples';
import { MDX } from '@/components/MDX';
Expand Down
2 changes: 1 addition & 1 deletion docs/app/components/server/chip/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Default } from '@/app/components/server/chip/examples/Default';
import ExampleSection from '@/app/components/shared/ExampleSection';
import ExampleSection from '@/app/components/shared/exampleSection/ExampleSection';
import QuickNav from '@/app/components/shared/QuickNav';
import { getExamples } from '@/app/utils/getExamples';
import { MDX } from '@/components/MDX';
Expand Down
2 changes: 1 addition & 1 deletion docs/app/components/server/input/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Default } from '@/app/components/server/input/examples/Default';
import ExampleSection from '@/app/components/shared/ExampleSection';
import ExampleSection from '@/app/components/shared/exampleSection/ExampleSection';
import QuickNav from '@/app/components/shared/QuickNav';
import { getExamples } from '@/app/utils/getExamples';
import { MDX } from '@/components/MDX';
Expand Down
2 changes: 1 addition & 1 deletion docs/app/components/server/loader/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Default } from '@/app/components/server/loader/examples/Default';
import ExampleSection from '@/app/components/shared/ExampleSection';
import ExampleSection from '@/app/components/shared/exampleSection/ExampleSection';
import QuickNav from '@/app/components/shared/QuickNav';
import { getExamples } from '@/app/utils/getExamples';
import { MDX } from '@/components/MDX';
Expand Down
2 changes: 1 addition & 1 deletion docs/app/components/server/nativeSelect/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Default } from '@/app/components/server/nativeSelect/examples/Default';
import ExampleSection from '@/app/components/shared/ExampleSection';
import ExampleSection from '@/app/components/shared/exampleSection/ExampleSection';
import QuickNav from '@/app/components/shared/QuickNav';
import { getExamples } from '@/app/utils/getExamples';
import { MDX } from '@/components/MDX';
Expand Down
2 changes: 1 addition & 1 deletion docs/app/components/server/pagination/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Default } from '@/app/components/server/pagination/examples/Default';
import ExampleSection from '@/app/components/shared/ExampleSection';
import ExampleSection from '@/app/components/shared/exampleSection/ExampleSection';
import QuickNav from '@/app/components/shared/QuickNav';
import { getExamples } from '@/app/utils/getExamples';
import { MDX } from '@/components/MDX';
Expand Down
2 changes: 1 addition & 1 deletion docs/app/components/server/table/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Default } from '@/app/components/server/table/examples/Default';
import ExampleSection from '@/app/components/shared/ExampleSection';
import ExampleSection from '@/app/components/shared/exampleSection/ExampleSection';
import QuickNav from '@/app/components/shared/QuickNav';
import { getExamples } from '@/app/utils/getExamples';
import { MDX } from '@/components/MDX';
Expand Down
2 changes: 1 addition & 1 deletion docs/app/components/server/tabs/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Default } from '@/app/components/server/tabs/examples/Default';
import ExampleSection from '@/app/components/shared/ExampleSection';
import ExampleSection from '@/app/components/shared/exampleSection/ExampleSection';
import QuickNav from '@/app/components/shared/QuickNav';
import { getExamples } from '@/app/utils/getExamples';
import { MDX } from '@/components/MDX';
Expand Down
2 changes: 1 addition & 1 deletion docs/app/components/server/tag/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Default } from '@/app/components/server/tag/examples/Default';
import ExampleSection from '@/app/components/shared/ExampleSection';
import ExampleSection from '@/app/components/shared/exampleSection/ExampleSection';
import QuickNav from '@/app/components/shared/QuickNav';
import { getExamples } from '@/app/utils/getExamples';
import { MDX } from '@/components/MDX';
Expand Down
73 changes: 0 additions & 73 deletions docs/app/components/shared/CodePreviewWrapper.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { mergeClassnames } from '@heathmont/moon-base-tw';
import { GenericLink } from '@heathmont/moon-icons-tw';
import CodePreview from './CodePreview';
import CodePreview from './codePreview/CodePreview';
import ComponentPreview from './ComponentPreview';
import HeaderSection from './HeaderSection';

type Props = {
title: string;
Expand All @@ -17,18 +17,7 @@ const ExampleSection: SC<Props> = async ({
code,
}) => (
<div className={mergeClassnames('flex flex-col gap-4 relative')}>
<h2 id={title} className="text-moon-20 font-medium ">
<a
href={`#${title}`}
className="flex items-center gap-3 [&:hover_svg]:opacity-100 cursor-pointer"
>
{title}
<GenericLink className="text-piccolo text-moon-16 opacity-0 transition-opacity" />
</a>
</h2>
{description && (
<div className="text-moon-16 text-bulma">{description}</div>
)}
<HeaderSection title={title} description={description} />
<div className="bg-gohan rounded-moon-i-sm overflow-hidden mt-2">
<ComponentPreview component={component} />
<CodePreview code={code} />
Expand Down
24 changes: 24 additions & 0 deletions docs/app/components/shared/exampleSection/HeaderSection.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { GenericLink } from '@heathmont/moon-icons-tw';

type Props = {
title?: string;
description?: string | JSX.Element;
};
const HeaderSection = ({ title, description }: Props) => (
<>
<h2 id={title} className="text-moon-20 font-medium ">
<a
href={`#${title}`}
className="flex items-center gap-3 [&:hover_svg]:opacity-100 cursor-pointer"
>
{title}
<GenericLink className="text-piccolo text-moon-16 opacity-0 transition-opacity" />
</a>
</h2>
{description && (
<div className="text-moon-16 text-bulma">{description}</div>
)}
</>
);

export default HeaderSection;
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ const CodeCopy = ({ code }: { code: string }) => {
);

const copyCode = () => {
console.log('copyCode', code);
if (navigator?.clipboard) {
navigator.clipboard.writeText(code ? code : '');
openSnackbarHandler('top-right');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import CodePreviewWrapper from './CodePreviewWrapper';
import CodePreviewWrapper from './wrapper/CodePreviewWrapper';

const CodePreview = ({ code }: { code: string }) => {
return (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use client';

import { useState, useEffect, useRef } from 'react';
import { mergeClassnames } from '@heathmont/moon-base-tw';
import ToggleCodeBtn from './ToggleCodeBtn';
import CodeCopy from '../CodeCopy';

const CodePreviewWrapper = ({
children,
code,
}: {
children?: React.ReactNode;
code: string;
}) => {
const [expand, setExpand] = useState(false);
const [height, setHeight] = useState(0);
const wrapperRef = useRef<HTMLDivElement>(null);
const clickHandler = () => {
setExpand(!expand);
};
useEffect(() => {
if (wrapperRef) {
setHeight(wrapperRef?.current?.querySelector('#code')?.clientHeight || 0);
}
}, []);
return (
<div
ref={wrapperRef}
className={mergeClassnames(
'relative text-bulma p-4 pb-0 md:pr-12 overflow-hidden border border-t-0 border-beerus rounded-b-moon-s-sm'
)}
>
<div
className={mergeClassnames(
'max-h-32 transition-[max-height] duration-150 ease-in-out overflow-hidden overflow-x-scroll pb-10 pt-10 md:pt-0',
expand && 'max-h-96 overflow-scroll'
)}
>
{children}
</div>
<CodeCopy code={code} />
{height > 72 && (
<ToggleCodeBtn expand={expand} clickHandler={clickHandler} />
)}
</div>
);
};

export default CodePreviewWrapper;
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { mergeClassnames } from '@heathmont/moon-base-tw';
import { ControlsChevronDownSmall } from '@heathmont/moon-icons-tw';

type Props = {
expand?: boolean;
clickHandler?: () => void;
};

const ToggleCodeBtn = ({ expand, clickHandler }: Props) => {
return (
<div
className={mergeClassnames(
'absolute bottom-0 inset-x-0 h-32 flex items-end z-0 bg-[linear-gradient(180deg,_rgba(249,_250,_251,_0.00)_0%,_#F9FAFB_100%)]',
expand && 'h-10'
)}
>
<button
onClick={clickHandler}
className={mergeClassnames(
'px-4 h-10 py-2 gap-2 text-moon-14 rounded-b-moon-s-sm relative z-0 flex justify-center items-center font-medium',
'no-underline overflow-hidden whitespace-nowrap select-none transition duration-200 hover:bg-heles text-bulma hover:text-bulma w-full',
expand ? 'bg-transperent' : 'bg-[rgba(249,_250,_251,_0.6)]'
)}
>
<span className="flex items-center justify-center gap-2">
{expand ? 'Collapsed code' : 'Expand code '}
<ControlsChevronDownSmall
className={mergeClassnames('text-moon-24', expand && 'rotate-180')}
/>
</span>
</button>
</div>
);
};

export default ToggleCodeBtn;

0 comments on commit fc99df8

Please sign in to comment.