Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ exports[`Gamut Exported Keys 1`] = `
"DataList",
"DataTable",
"DelayedRenderWrapper",
"DetailedCode",
"Dialog",
"Disclosure",
"Drawer",
Expand Down
19 changes: 19 additions & 0 deletions packages/gamut/src/DetailedCode/DetailedCodeBody/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Source } from '@storybook/blocks';
import { ComponentProps } from 'react';
import * as React from 'react';

import { DetailedCodeBodyWrapper } from '../elements';
import { DetailedCodeBodyProps } from '../types';

type SourceLanguage = ComponentProps<typeof Source>['language'];

export const DetailedCodeBody: React.FC<DetailedCodeBodyProps> = ({
code,
language,
}) => {
return (
<DetailedCodeBodyWrapper>
<Source code={code} dark language={language as SourceLanguage} />
</DetailedCodeBodyWrapper>
);
};
43 changes: 43 additions & 0 deletions packages/gamut/src/DetailedCode/DetailedCodeButton/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { MiniChevronDownIcon } from '@codecademy/gamut-icons';
import * as React from 'react';

import { Anchor } from '../../Anchor';
import { Rotation } from '../../Animation';
import { FlexBox } from '../../Box';
import { Text } from '../../Typography';
import { DetailedCodeButtonProps } from '../types';

export const DetailedCodeButton: React.FC<DetailedCodeButtonProps> = ({
isExpanded,
setIsExpanded,
}) => {
const handleClick = () => {
if (setIsExpanded) {
setIsExpanded((prev: boolean) => !prev);
}
};

return (
<Anchor
aria-expanded={isExpanded}
height="100%"
px={16}
py={12}
variant="interface"
width="100%"
onClick={handleClick}
>
<FlexBox
columnGap={16}
flexDirection="row"
justifyContent="space-between"
width="100%"
>
<Text>{isExpanded ? 'Show Less Code' : 'Show More Code'}</Text>
<Rotation height={16} rotated={isExpanded} width={16}>
<MiniChevronDownIcon aria-hidden size={16} />
</Rotation>
</FlexBox>
</Anchor>
);
};
25 changes: 25 additions & 0 deletions packages/gamut/src/DetailedCode/elements.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { css } from '@codecademy/gamut-styles';
import styled from '@emotion/styled';

import { FlexBox } from '../Box';

export const DetailedCodeWrapper = styled(FlexBox)(
css({
width: '100%',
flexDirection: 'column',
borderRadius: 'md',
border: 1,
bg: 'background',
})
);

export const DetailedCodeBodyWrapper = styled(FlexBox)(
css({
flexDirection: 'column',
/* Override Storybook's Source component default styles to remove unwanted spacing and borders in the container */
'& .docblock-source': {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also did some styling testing and it seems like targeting the .docblock-source is the most consistent want to apply the styling :\ so that seems to be just the way to do it.

but I think it warrants adding a note for why it has to be done this way.

borderRadius: 'none',
margin: 0,
},
})
);
51 changes: 51 additions & 0 deletions packages/gamut/src/DetailedCode/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import React, { useState } from 'react';

import { DetailedCodeBody } from './DetailedCodeBody';
import { DetailedCodeButton } from './DetailedCodeButton';
import { DetailedCodeWrapper } from './elements';
import { DetailedCodeProps } from './types';

const DEFAULT_PREVIEW_LINES = 10;
const DEFAULT_LANGUAGE = 'tsx';

const getPreviewCode = (code: string, previewLines: number) => {
const lines = code.split('\n');

if (lines.length <= previewLines) {
return code;
}

return lines.slice(0, previewLines).join('\n');
};

export const DetailedCode: React.FC<DetailedCodeProps> = ({
code,
initiallyExpanded = false,
language = DEFAULT_LANGUAGE,
preview = false,
previewLines = DEFAULT_PREVIEW_LINES,
}) => {
const [isExpanded, setIsExpanded] = useState(initiallyExpanded);
const normalizedPreviewLines = Math.max(0, previewLines);
const previewEnabled = preview && normalizedPreviewLines > 0;
const previewCode = previewEnabled
? getPreviewCode(code, normalizedPreviewLines)
: code;

const hasMoreCode =
previewEnabled && code.split('\n').length > normalizedPreviewLines;

const displayedCode = isExpanded ? code : previewCode;

return (
<DetailedCodeWrapper>
<DetailedCodeBody code={displayedCode} language={language} />
{hasMoreCode && (
<DetailedCodeButton
isExpanded={isExpanded}
setIsExpanded={setIsExpanded}
/>
)}
</DetailedCodeWrapper>
);
};
17 changes: 17 additions & 0 deletions packages/gamut/src/DetailedCode/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export interface DetailedCodeProps {
code: string;
language: string;
initiallyExpanded?: boolean;
preview?: boolean;
previewLines?: number;
}

export interface DetailedCodeButtonProps {
isExpanded?: boolean;
setIsExpanded?: React.Dispatch<React.SetStateAction<boolean>>;
}

export interface DetailedCodeBodyProps {
code: string;
language: string;
}
1 change: 1 addition & 0 deletions packages/gamut/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export * from './Coachmark';
export * from './ConnectedForm';
export * from './ContentContainer';
export * from './DelayedRenderWrapper';
export * from './DetailedCode';
export * from './Disclosure';
export * from './DataList';
export * from './Drawer';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { DetailedCode } from '@codecademy/gamut';
import { Canvas, Controls, Meta } from '@storybook/blocks';

import { ComponentHeader, LinkTo } from '~styleguide/blocks';

import * as ConnectedFormStories from './ConnectedForm.stories';
import { example } from './example';

export const parameters = {
title: 'ConnectedForm',
Expand Down Expand Up @@ -40,75 +42,7 @@ This hook also returns the `FormRequiredText` component - include this before yo

### Example code

```tsx
import {
ConnectedCheckbox,
ConnectedInput,
ConnectedSelect,
useConnectedForm,
} from '@codecademy/gamut';

import { TerminalIcon } from '@codecademy/gamut-icons';

export const GoodForm = () => {
const {
ConnectedFormGroup,
ConnectedForm,
connectedFormProps,
FormRequiredText,
} = useConnectedForm({
defaultValues: {
thisField: true,
thatField: 'zero',
anotherField: 'state your name.',
},
validationRules: {
thisField: { required: 'you need to check this.' },
thatField: {
pattern: {
value: /^(?:(?!zero).)*$/,
message: 'literally anything but zero',
},
},
},
});

return (
<ConnectedForm
onSubmit={({ thisField }) => console.log(thisField)}
resetOnSubmit
{...connectedFormProps}
>
<SubmitButton>submit this form.</SubmitButton>
<ConnectedFormGroup
name="thisField"
label="cool checkbox bruh"
field={{
component: ConnectedCheckbox,
label: 'check it ouuut',
}}
/>
<ConnectedFormGroup
name="thatField"
label="cool select dude"
field={{
component: ConnectedSelect,
options: ['one', 'two', 'zero'],
}}
/>
<ConnectedFormGroup
name="anotherField"
label="cool input"
field={{
component: ConnectedInput,
icon: TerminalIcon,
}}
/>
<FormRequiredText />
</ConnectedForm>
);
};
```
<DetailedCode code={example} preview />

## Variants

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
export const example = `import {
ConnectedCheckbox,
ConnectedInput,
ConnectedSelect,
useConnectedForm,
} from '@codecademy/gamut';

import { TerminalIcon } from '@codecademy/gamut-icons';

export const GoodForm = () => {
const {
ConnectedFormGroup,
ConnectedForm,
connectedFormProps,
FormRequiredText,
} = useConnectedForm({
defaultValues: {
thisField: true,
thatField: 'zero',
anotherField: 'state your name.',
},
validationRules: {
thisField: { required: 'you need to check this.' },
thatField: {
pattern: {
value: /^(?:(?!zero).)*$/,
message: 'literally anything but zero',
},
},
},
});

return (
<ConnectedForm
onSubmit={({ thisField }) => console.log(thisField)}
resetOnSubmit
{...connectedFormProps}
>
<SubmitButton>submit this form.</SubmitButton>
<ConnectedFormGroup
name="thisField"
label="cool checkbox bruh"
field={{
component: ConnectedCheckbox,
label: 'check it ouuut',
}}
/>
<ConnectedFormGroup
name="thatField"
label="cool select dude"
field={{
component: ConnectedSelect,
options: ['one', 'two', 'zero'],
}}
/>
<ConnectedFormGroup
name="anotherField"
label="cool input"
field={{
component: ConnectedInput,
icon: TerminalIcon,
}}
/>
<FormRequiredText />
</ConnectedForm>
);
};`;