-
Notifications
You must be signed in to change notification settings - Fork 31
feat: Detailed code component for longer code examples #3236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nhivpham
wants to merge
20
commits into
main
Choose a base branch
from
np-gmt-257
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
0f23814
Added first implementation, dropdown
nhivpham a95a191
Try preview
nhivpham b688abc
Merge branch 'main' into np-gmt-257
nhivpham ac512c6
move button to bottom and remove weird styling visual issue
nhivpham 51d159d
update for linter
nhivpham d0a7dad
update test snapshot
nhivpham 2711ba7
add helper in inconsistent styling
nhivpham ccc004e
Incorporate feedback
nhivpham 6f8e4b9
Merge branch 'main' into np-gmt-257
nhivpham d488790
Merge branch 'main' into np-gmt-257
nhivpham ade841a
move component into folder, add floating button, small type safety tweak
nhivpham b7cff3f
update badge to ellipses, update some typings, add anchor back
nhivpham 0394239
remove prop drill, duplicate line count, and extra styles
nhivpham bb75a84
Merge branch 'main' into np-gmt-257
nhivpham 9a52768
prettier again for the 100th time
nhivpham da8b65f
feedback + code lines?
nhivpham ada707c
Merge branch 'main' into np-gmt-257
nhivpham 25c9f86
update to sb10, fix moved overlay, address line count
nhivpham 2d61914
clean up stylings
nhivpham 5d10a0a
updated slightly confusing variable names and add an extra example
nhivpham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
19 changes: 19 additions & 0 deletions
19
packages/gamut/src/DetailedCode/DetailedCodeBody/index.tsx
This file contains hidden or 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,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 column> | ||
| <Source code={code} dark language={language as SourceLanguage} /> | ||
| </DetailedCodeBodyWrapper> | ||
| ); | ||
| }; | ||
70 changes: 70 additions & 0 deletions
70
packages/gamut/src/DetailedCode/DetailedCodeButton/index.tsx
This file contains hidden or 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,70 @@ | ||
| import { MiniChevronDownIcon } from '@codecademy/gamut-icons'; | ||
| import * as React from 'react'; | ||
|
|
||
| import { Rotation } from '../../Animation'; | ||
| import { Box, FlexBox } from '../../Box'; | ||
| import { Text } from '../../Typography'; | ||
| import { DetailedCodeButtonWrapper } from '../elements'; | ||
| import { DetailedCodeButtonProps } from '../types'; | ||
|
|
||
| export const DetailedCodeButton: React.FC<DetailedCodeButtonProps> = ({ | ||
| heading, | ||
| isExpanded, | ||
| language, | ||
|
LinKCoding marked this conversation as resolved.
Outdated
|
||
| setIsExpanded, | ||
| }) => { | ||
| const handleClick = () => { | ||
| if (setIsExpanded) { | ||
| setIsExpanded((prev: boolean) => !prev); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <FlexBox> | ||
|
nhivpham marked this conversation as resolved.
Outdated
|
||
| <DetailedCodeButtonWrapper | ||
| aria-expanded={isExpanded} | ||
| height="100%" | ||
| px={16} | ||
| py={12} | ||
| variant="interface" | ||
| width="100%" | ||
| onClick={handleClick} | ||
| > | ||
| <FlexBox | ||
| alignItems="center" | ||
| columnGap={16} | ||
| flexDirection="row" | ||
| justifyContent="space-between" | ||
| width="100%" | ||
| > | ||
| <FlexBox alignItems="center" columnGap={12}> | ||
| {heading && ( | ||
|
nhivpham marked this conversation as resolved.
Outdated
|
||
| <Text | ||
| as="h3" | ||
| fontWeight="title" | ||
| p={0} | ||
| textAlign="start" | ||
| variant="title-xs" | ||
| > | ||
| {heading} | ||
| </Text> | ||
| )} | ||
| <Text | ||
| color="text-secondary" | ||
| fontFamily="monospace" | ||
| variant="p-small" | ||
| > | ||
| {language} | ||
| </Text> | ||
| </FlexBox> | ||
|
|
||
| <Box p={8}> | ||
| <Rotation height={16} rotated={isExpanded} width={16}> | ||
| <MiniChevronDownIcon aria-hidden size={16} /> | ||
| </Rotation> | ||
|
LinKCoding marked this conversation as resolved.
Outdated
|
||
| </Box> | ||
| </FlexBox> | ||
| </DetailedCodeButtonWrapper> | ||
| </FlexBox> | ||
| ); | ||
| }; | ||
This file contains hidden or 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,38 @@ | ||
| import { css } from '@codecademy/gamut-styles'; | ||
| import styled from '@emotion/styled'; | ||
|
|
||
| import { Anchor } from '../Anchor'; | ||
| import { FlexBox } from '../Box'; | ||
|
|
||
| export const DetailedCodeWrapper = styled(FlexBox)( | ||
| css({ | ||
|
nhivpham marked this conversation as resolved.
Outdated
|
||
| width: '100%', | ||
| maxHeight: 'fit-content', | ||
| borderRadius: 'md', | ||
| border: 1, | ||
| bg: 'background', | ||
| }) | ||
| ); | ||
|
|
||
| export const DetailedCodeButtonWrapper = styled(Anchor)( | ||
| css({ | ||
| borderRadius: 'md', | ||
| bg: 'inherit', | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these properties used? I didn't see anything that stood out immediately |
||
| }) | ||
| ); | ||
|
|
||
| export const DetailedCodeBodyWrapper = styled(FlexBox)( | ||
| css({ | ||
| overflow: 'hidden', | ||
| '& pre': { | ||
| margin: 0, | ||
| }, | ||
| '& > div': { | ||
| borderRadius: 'none', | ||
| padding: 0, | ||
| }, | ||
|
LinKCoding marked this conversation as resolved.
Outdated
|
||
| '& .docblock-source': { | ||
|
LinKCoding marked this conversation as resolved.
Outdated
|
||
| margin: 0, | ||
| }, | ||
| }) | ||
| ); | ||
This file contains hidden or 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,55 @@ | ||
| import { useState } from 'react'; | ||
| import * as React from 'react'; | ||
|
|
||
| import { DetailedCodeBody } from './DetailedCodeBody'; | ||
| import { DetailedCodeButton } from './DetailedCodeButton'; | ||
| import { DetailedCodeWrapper } from './elements'; | ||
| import { DetailedCodeProps } from './types'; | ||
|
|
||
| const DEFAULT_PREVIEW_LINES = 10; | ||
|
|
||
| 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, | ||
| heading, | ||
| initiallyExpanded = false, | ||
| 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; | ||
|
|
||
| // Show the button to expand the code if there is more code than the preview lines, and hide the button if there is no more code | ||
|
nhivpham marked this conversation as resolved.
Outdated
|
||
| const hasMoreCode = | ||
| previewEnabled && code.split('\n').length > normalizedPreviewLines; | ||
|
|
||
| const displayedCode = isExpanded ? code : previewCode; | ||
|
|
||
| return ( | ||
| <DetailedCodeWrapper column> | ||
| <DetailedCodeBody code={displayedCode} language={language} /> | ||
| {hasMoreCode && ( | ||
| <DetailedCodeButton | ||
| heading={heading} | ||
| isExpanded={isExpanded} | ||
| language={language} | ||
| setIsExpanded={setIsExpanded} | ||
| /> | ||
| )} | ||
| </DetailedCodeWrapper> | ||
| ); | ||
| }; | ||
This file contains hidden or 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,20 @@ | ||
| export interface DetailedCodeProps { | ||
| code: string; | ||
| heading?: string; | ||
| language: string; | ||
| initiallyExpanded?: boolean; | ||
| preview?: boolean; | ||
| previewLines?: number; | ||
| } | ||
|
|
||
| export interface DetailedCodeButtonProps { | ||
| heading?: string; | ||
| isExpanded?: boolean; | ||
| language: string; | ||
| setIsExpanded?: React.Dispatch<React.SetStateAction<boolean>>; | ||
| } | ||
|
|
||
| export interface DetailedCodeBodyProps { | ||
| code: string; | ||
| language: string; | ||
| } |
This file contains hidden or 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 hidden or 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
67 changes: 67 additions & 0 deletions
67
packages/styleguide/src/lib/Organisms/ConnectedForm/ConnectedForm/codeExample.ts
This file contains hidden or 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,67 @@ | ||
| export const codeExample = `import { | ||
|
nhivpham marked this conversation as resolved.
Outdated
|
||
| 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> | ||
| ); | ||
| };`; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.