Skip to content
Open
Show file tree
Hide file tree
Changes from 15 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
@@ -0,0 +1,19 @@
import { Source } from '@storybook/blocks';

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

export const DetailedCodeBody: React.FC<DetailedCodeBodyProps> = ({
code,
language,
showFloatingBadge = false,
}) => {
return (
<DetailedCodeBodyWrapper hasFloatingBadge={showFloatingBadge}>
<Source code={code} dark language={language} />
{showFloatingBadge && (
<FloatingIndicator aria-label="More code below">...</FloatingIndicator>
Comment thread
nhivpham marked this conversation as resolved.
Outdated
)}
</DetailedCodeBodyWrapper>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { MiniChevronDownIcon } from '@codecademy/gamut-icons';
import * as React from 'react';
import { Anchor, Rotation, FlexBox, Text } from '@codecademy/gamut';

import { DetailedCodeButtonProps } from '../types';

export const DetailedCodeButton: React.FC<DetailedCodeButtonProps> = ({
isExpanded,
onToggle,
language,
}) => {
return (
<Anchor
aria-expanded={isExpanded}
px={16}
py={12}
variant="interface"
width="100%"
onClick={onToggle}
>
<FlexBox columnGap={16} justifyContent="space-between">
<Text>{language}</Text>
<FlexBox columnGap={8} flexDirection="row" alignItems="center">
<Text>{isExpanded ? 'Show Less Code' : 'Show More Code'}</Text>
<Rotation rotated={isExpanded}>
<MiniChevronDownIcon aria-hidden size={16} />
</Rotation>
</FlexBox>
</FlexBox>
</Anchor>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { css } from '@codecademy/gamut-styles';
import styled from '@emotion/styled';
import { FlexBox } from '@codecademy/gamut';

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

export const DetailedCodeBodyWrapper = styled(FlexBox)<{
hasFloatingBadge?: boolean;
Comment thread
nhivpham marked this conversation as resolved.
Outdated
}>(({ hasFloatingBadge }) =>
css({
position: 'relative',
flexDirection: 'column',
/* Override Storybook's Source component default styles to remove unwanted spacing and borders in the container */
'& .docblock-source': {
borderRadius: 'none',
margin: 0,
pb: hasFloatingBadge ? 48 : 0,
Comment thread
nhivpham marked this conversation as resolved.
Outdated
},
})
);

export const FloatingIndicator = styled(FlexBox)(
Comment thread
nhivpham marked this conversation as resolved.
Outdated
css({
position: 'absolute',
bottom: 16,
left: '50%',
transform: 'translateX(-50%)',
zIndex: 1,
bg: 'inherit',
px: 12,
py: 4,
borderRadius: 'lg',
Comment thread
nhivpham marked this conversation as resolved.
Outdated
fontSize: 26,
fontWeight: 700,
color: 'white',
Comment thread
nhivpham marked this conversation as resolved.
Outdated
letterSpacing: '0.1em',
})
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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 = 20;
const DEFAULT_LANGUAGE = 'tsx';

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 codeLines = code.split('\n');
const hasMoreCode =
previewEnabled && codeLines.length > normalizedPreviewLines;

const previewCode = previewEnabled
? codeLines.slice(0, normalizedPreviewLines).join('\n')
: code;

const displayedCode = isExpanded ? code : previewCode;

return (
<DetailedCodeWrapper>
<DetailedCodeBody
code={displayedCode}
language={language}
showFloatingBadge={hasMoreCode && !isExpanded}
/>
{hasMoreCode && (
<DetailedCodeButton
isExpanded={isExpanded}
onToggle={() => setIsExpanded((prev) => !prev)}
language={language}
/>
)}
</DetailedCodeWrapper>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Source } from '@storybook/blocks';
import { ComponentProps } from 'react';

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

export interface DetailedCodeProps {
code: string;
language?: SourceLanguage;
initiallyExpanded?: boolean;
preview?: boolean;
previewLines?: number;
Comment on lines +7 to +11
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For these types, and the other types as well, I'd recommend adding comments to explain what the prop is for/doing

see: packages/gamut/src/Disclosure/types.ts as an example
Fwiw, this is def something you can prompt the robot to do

}

export interface DetailedCodeButtonProps {
isExpanded: boolean;
onToggle: () => void;
language: SourceLanguage;
}

export interface DetailedCodeBodyProps {
code: string;
language: SourceLanguage;
showFloatingBadge?: boolean;
}
1 change: 1 addition & 0 deletions packages/styleguide/.storybook/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './ImageWrapper';
export * from './TokenTable';
export * from './Headers';
export * from './Elements/Callout';
export * from './Elements/DetailedCode';
export * from './Elements/KeyboardKey';
export * from './Elements/Markdown';
export * from './Elements/ImageGallery';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Canvas, Controls, Meta } from '@storybook/blocks';

import { ComponentHeader, LinkTo } from '~styleguide/blocks';
import { ComponentHeader, DetailedCode, 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 +41,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} language="tsx" 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,
Copy link

Copilot AI Mar 23, 2026

Choose a reason for hiding this comment

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

This code example references <SubmitButton> later, but the import block in the snippet doesn't include it. Since this is intended to be copy/pastable example code, please add SubmitButton to the @codecademy/gamut import list (or remove the usage).

Suggested change
ConnectedSelect,
ConnectedSelect,
SubmitButton,

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This seems valid fwiw, @nhivpham

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>
);
};`;
Loading