Skip to content

Commit

Permalink
feat: swap site codeblock for our code block (#3171)
Browse files Browse the repository at this point in the history
* feat: swap site codeblock for our code block

* feat(inline-code): match default to minimal styles and hide minimal

* chore: remove last mention of minimal inline code

* chore: inherit text color on inline code for linked code`
  • Loading branch information
SiTaggart committed May 11, 2023
1 parent 98fe875 commit 0d57f59
Show file tree
Hide file tree
Showing 20 changed files with 135 additions and 226 deletions.
6 changes: 6 additions & 0 deletions .changeset/fuzzy-bats-exercise.md
@@ -0,0 +1,6 @@
---
'@twilio-paste/inline-code': patch
'@twilio-paste/core': patch
---

[Inline Code] Design upate to the default inline code to match the minimal styles with a view to deprecating "blue" inline code styles
32 changes: 17 additions & 15 deletions packages/paste-core/components/inline-code/src/index.tsx
@@ -1,35 +1,35 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import {Box, safelySpreadBoxProps} from '@twilio-paste/box';
import type {BoxProps, BoxStyleProps} from '@twilio-paste/box';
import type {BoxProps} from '@twilio-paste/box';

/**
* Inline Code style variants.
* Minimal is a deprecated vairant that we now hide, but we can't remove as it's a breaking change
*/
type InlineCodeVariants = 'default' | 'minimal';

export interface InlineCodeProps extends Partial<Omit<HTMLDivElement, 'children'>> {
children: string;
element?: BoxProps['element'];

/**
* Choose the badge variant you would like to use.
* Please not the `minimal` variant is now deprecated
*
* @type {InlineCodeVariants}
* @memberof InlineCodeProps
*/
variant?: InlineCodeVariants;
}

const variantStyles: Record<InlineCodeVariants, BoxStyleProps> = {
default: {
color: 'colorTextPrimaryStronger',
backgroundColor: 'colorBackgroundPrimaryWeakest',
borderColor: 'colorBorderPrimaryWeaker',
},
minimal: {
color: 'colorTextWeak',
backgroundColor: 'colorBackgroundWeak',
borderColor: 'colorBorderWeak',
},
};

const InlineCode = React.forwardRef<HTMLDivElement, InlineCodeProps>(
({children, element = 'INLINE_CODE', variant = 'default', ...props}, ref) => {
return (
<Box
as="code"
element={element}
// even though we deprecated the minimal variant we can't remove setting variant because a consumer maybe targeting it for customization
variant={variant}
{...safelySpreadBoxProps(props)}
borderWidth="borderWidth10"
Expand All @@ -39,7 +39,9 @@ const InlineCode = React.forwardRef<HTMLDivElement, InlineCodeProps>(
fontSize="inherit"
lineHeight="inherit"
paddingX="space20"
{...variantStyles[variant]}
color="inherit"
backgroundColor="colorBackgroundWeak"
borderColor="colorBorderWeak"
ref={ref}
>
{children}
Expand Down
Expand Up @@ -19,10 +19,6 @@ export const Default: StoryFn = () => {
return <InlineCode>0000001111111122234444444</InlineCode>;
};

export const Minimal: StoryFn = () => {
return <InlineCode variant="minimal">0000001111111122234444444</InlineCode>;
};

export const ReallyLongContent: StoryFn = () => {
return (
<>
Expand Down
32 changes: 24 additions & 8 deletions packages/paste-website/package.json
Expand Up @@ -38,38 +38,51 @@
"@twilio-paste/box": "^9.0.0",
"@twilio-paste/breadcrumb": "^10.0.0",
"@twilio-paste/button": "^13.0.0",
"@twilio-paste/button-group": "^3.0.0",
"@twilio-paste/callout": "^3.0.0",
"@twilio-paste/card": "^8.0.0",
"@twilio-paste/chat-composer": "^3.0.0",
"@twilio-paste/chat-log": "^4.0.0",
"@twilio-paste/checkbox": "^12.0.0",
"@twilio-paste/clipboard-copy-library": "^2.0.0",
"@twilio-paste/code-block": "^3.0.0",
"@twilio-paste/color-contrast-utils": "^4.0.0",
"@twilio-paste/combobox": "^15.0.0",
"@twilio-paste/combobox": "^15.0.1",
"@twilio-paste/combobox-primitive": "^1.0.0",
"@twilio-paste/customization": "^7.0.0",
"@twilio-paste/data-grid": "^7.0.0",
"@twilio-paste/data-visualization-library": "^4.0.0",
"@twilio-paste/date-picker": "^5.0.0",
"@twilio-paste/design-tokens": "^9.0.2",
"@twilio-paste/description-list": "^3.0.0",
"@twilio-paste/design-tokens": "^9.0.3",
"@twilio-paste/detail-text": "^2.0.0",
"@twilio-paste/disclosure": "^11.0.0",
"@twilio-paste/disclosure-primitive": "^1.0.0",
"@twilio-paste/display-heading": "^3.0.0",
"@twilio-paste/display-pill-group": "^7.0.0",
"@twilio-paste/dropdown-library": "^2.0.0",
"@twilio-paste/file-picker": "^3.0.0",
"@twilio-paste/file-uploader": "^3.0.0",
"@twilio-paste/flex": "^7.0.0",
"@twilio-paste/form": "^10.0.0",
"@twilio-paste/form-pill-group": "^7.0.0",
"@twilio-paste/grid": "^7.0.0",
"@twilio-paste/heading": "^10.0.0",
"@twilio-paste/help-text": "^12.0.0",
"@twilio-paste/icons": "^11.0.0",
"@twilio-paste/icons": "11.0.0",
"@twilio-paste/in-page-navigation": "^3.0.0",
"@twilio-paste/inline-code": "^3.0.0",
"@twilio-paste/inline-control-group": "^12.0.0",
"@twilio-paste/input": "^8.0.0",
"@twilio-paste/input-box": "^9.0.0",
"@twilio-paste/label": "^12.0.0",
"@twilio-paste/label": "^12.0.1",
"@twilio-paste/lexical-library": "^2.0.0",
"@twilio-paste/list": "^7.0.0",
"@twilio-paste/listbox-primitive": "^2.0.0",
"@twilio-paste/media-object": "^9.0.0",
"@twilio-paste/menu": "^13.0.0",
"@twilio-paste/menu-primitive": "^1.0.0",
"@twilio-paste/minimizable-dialog": "^3.0.0",
"@twilio-paste/modal": "^15.0.0",
"@twilio-paste/modal-dialog-primitive": "^1.0.0",
"@twilio-paste/non-modal-dialog-primitive": "^1.0.0",
Expand All @@ -78,23 +91,26 @@
"@twilio-paste/popover": "^12.0.0",
"@twilio-paste/radio-button-group": "^3.0.0",
"@twilio-paste/radio-group": "^12.0.0",
"@twilio-paste/react-textarea-autosize-library": "^2.0.0",
"@twilio-paste/reakit-library": "^1.0.0",
"@twilio-paste/screen-reader-only": "^12.0.0",
"@twilio-paste/select": "^11.0.0",
"@twilio-paste/select": "^11.0.1",
"@twilio-paste/separator": "^7.0.0",
"@twilio-paste/sibling-box": "^8.0.0",
"@twilio-paste/side-modal": "^3.0.0",
"@twilio-paste/side-modal": "^3.0.1",
"@twilio-paste/skeleton-loader": "^5.0.0",
"@twilio-paste/spinner": "^13.0.0",
"@twilio-paste/stack": "^7.0.0",
"@twilio-paste/style-props": "^8.0.0",
"@twilio-paste/styling-library": "^2.0.0",
"@twilio-paste/styling-library": "^2.0.1",
"@twilio-paste/switch": "^4.0.0",
"@twilio-paste/syntax-highlighter-library": "^2.0.0",
"@twilio-paste/table": "^7.0.0",
"@twilio-paste/tabs": "^7.0.0",
"@twilio-paste/tabs-primitive": "^1.0.0",
"@twilio-paste/text": "^9.0.0",
"@twilio-paste/textarea": "^9.0.0",
"@twilio-paste/theme": "^10.0.0",
"@twilio-paste/theme": "^10.0.1",
"@twilio-paste/time-picker": "^5.0.0",
"@twilio-paste/toast": "^11.0.0",
"@twilio-paste/tooltip": "^10.0.0",
Expand Down
7 changes: 3 additions & 4 deletions packages/paste-website/src/components/ThemeObjectDisplay.tsx
@@ -1,10 +1,9 @@
import * as React from 'react';
import {Box} from '@twilio-paste/box';
import {useTheme} from '@twilio-paste/theme';
import {CodeBlock} from '@twilio-paste/code-block';
import {format as prettyFormat} from 'pretty-format';

import {Codeblock} from './codeblock';

export const ThemeObjectDisplay: React.FC<React.PropsWithChildren<unknown>> = () => {
const theme = useTheme();
const themeKeys = Object.keys(theme);
Expand All @@ -13,8 +12,8 @@ export const ThemeObjectDisplay: React.FC<React.PropsWithChildren<unknown>> = ()
reducedTheme[key] = {};
});
return (
<Box as="pre" marginBottom="space70">
<Codeblock>{prettyFormat(reducedTheme)}</Codeblock>
<Box marginBottom="space70">
<CodeBlock code={prettyFormat(reducedTheme)} language="json" />
</Box>
);
};
27 changes: 1 addition & 26 deletions packages/paste-website/src/components/Typography.tsx
@@ -1,7 +1,6 @@
import * as React from 'react';
import {Text} from '@twilio-paste/text';
import {Box} from '@twilio-paste/box';
import type {BoxProps} from '@twilio-paste/box';

export interface PProps {
variant?: 'default' | 'lead';
Expand All @@ -20,29 +19,5 @@ export const P: React.FC<React.PropsWithChildren<PProps>> = ({children, variant}
};

export const Pre: React.FC<React.PropsWithChildren> = (props) => {
return (
<Text as="pre" fontSize="fontSize20" marginBottom="space70" lineHeight="lineHeight30">
{props.children}
</Text>
);
};

export const InlineCode: React.FC<React.PropsWithChildren<BoxProps>> = (props) => {
return (
<Box
as="code"
backgroundColor="colorBackground"
borderWidth="borderWidth10"
borderColor="colorBorderWeaker"
borderStyle="solid"
borderRadius="borderRadius20"
color="inherit"
fontFamily="fontFamilyCode"
fontSize="fontSize20"
lineHeight="lineHeight20"
paddingY="space10"
paddingX="space20"
{...props}
/>
);
return <Box marginBottom="space70">{props.children}</Box>;
};
100 changes: 14 additions & 86 deletions packages/paste-website/src/components/codeblock/index.tsx
@@ -1,90 +1,18 @@
import * as React from 'react';
import {styled, themeGet} from '@twilio-paste/styling-library';
import Highlight, {defaultProps} from 'prism-react-renderer';
import type {CodeBlockProps} from '@twilio-paste/code-block';
import {CodeBlock} from '@twilio-paste/code-block';

import {CodeblockTheme} from '../shortcodes/live-preview/theme';

const StyledCode = styled.code((props) => ({
display: 'block',
borderRadius: themeGet('radii.borderRadius20')(props),
height: '100%',
fontFamily: themeGet('fonts.fontFamilyCode')(props),
fontSize: 'inherit',
lineHeight: themeGet('lineHeights.lineHeight30')(props),
overflowX: 'auto',
padding: `${themeGet('space.space40')(props)} ${themeGet('space.space60')(props)}`,
whiteSpace: 'pre-wrap',
}));

export type Language =
| 'markup'
| 'bash'
| 'clike'
| 'c'
| 'cpp'
| 'css'
| 'javascript'
| 'jsx'
| 'coffeescript'
| 'actionscript'
| 'css-extr'
| 'diff'
| 'git'
| 'go'
| 'graphql'
| 'handlebars'
| 'json'
| 'less'
| 'makefile'
| 'markdown'
| 'objectivec'
| 'ocaml'
| 'python'
| 'reason'
| 'sass'
| 'scss'
| 'sql'
| 'stylus'
| 'typescript'
| 'wasm'
| 'yaml';

export interface CodeblockProps extends React.ComponentProps<'code'> {
children: string;
className?: string;
live?: boolean;
}

const Codeblock: React.FC<React.PropsWithChildren<CodeblockProps>> = ({children, className = 'language-jsx'}) => {
const language = className.replace(/language-/, '') as Language;

return (
<Highlight {...defaultProps} code={children} language={language} theme={CodeblockTheme}>
{({className: _className, style, tokens, getLineProps, getTokenProps}) => {
const lastLineIndex = tokens.length - 1;
return (
<StyledCode className={_className} style={style}>
{tokens.map((line, i) => {
// Codeblocks keep adding an extra blank line, this checks for that and removes it
if (i === lastLineIndex && line.length === 1 && (line[0].content === '' || line[0].empty)) {
return null;
}

return (
// eslint-disable-next-line react/no-array-index-key
<div key={`line-${i}`} {...getLineProps({line, key: i})}>
{line.map((token, key) => (
// eslint-disable-next-line react/no-array-index-key
<span key={`token-${key}`} {...getTokenProps({token, key})} />
))}
</div>
);
})}
</StyledCode>
);
}}
</Highlight>
);
export type CodeblockProps = CodeBlockProps & {
className: string;
};

export {Codeblock};
export const Codeblock: React.FC<React.PropsWithChildren<CodeblockProps>> = ({
children,
className = 'language-jsx',
}) => {
if (children == null) {
return null;
}
const language = className.replace(/language-/, '') as CodeBlockProps['language'];
return <CodeBlock code={children} language={language} />;
};
4 changes: 2 additions & 2 deletions packages/paste-website/src/components/icons-list/IconCard.tsx
Expand Up @@ -4,8 +4,8 @@ import {Heading} from '@twilio-paste/heading';
import {Box} from '@twilio-paste/box';
import {Text} from '@twilio-paste/text';
import {Paragraph} from '@twilio-paste/paragraph';
import {CodeBlock} from '@twilio-paste/code-block';

import {Codeblock} from '../codeblock';
import {CopyButton} from '../CopyButton';
import {SiteLink} from '../SiteLink';
import type {IconObject} from './types';
Expand Down Expand Up @@ -39,7 +39,7 @@ const IconCard: React.FC<React.PropsWithChildren<IconCardProps>> = ({selectedIco
Install using React
</Heading>
<Box marginBottom="space70" position="relative">
<Codeblock>{iconSnippet(selectedIcon.name)}</Codeblock>
<CodeBlock language="jsx" code={iconSnippet(selectedIcon.name)} />
<Box bottom="10px" position="absolute" right="10px">
<CopyButton text={iconSnippet(selectedIcon.name)} />
</Box>
Expand Down
Expand Up @@ -11,11 +11,12 @@ import {OrderedList, UnorderedList, ListItem} from '@twilio-paste/list';
import {Separator} from '@twilio-paste/separator';
import type {SeparatorProps} from '@twilio-paste/separator';
import {Table, THead, TBody, TFoot, Tr, Th, Td} from '@twilio-paste/table';
import {InlineCode} from '@twilio-paste/inline-code';

import {Codeblock} from '../codeblock';
import type {CodeblockProps} from '../codeblock';
import {Codeblock} from '../codeblock';
import {AnchoredHeading} from '../Heading';
import {InlineCode, Pre} from '../Typography';
import {Pre} from '../Typography';
// Shortcode imports
import {GenericHeader} from '../shortcodes/generic-header';
import {NormalizedPatternHeader} from '../shortcodes/normalized-pattern-header';
Expand Down
@@ -1,6 +1,7 @@
// https://github.com/FormidableLabs/react-live
import * as React from 'react';
import {LiveProvider, LiveEditor, LiveError, LivePreview as ReactLivePreview} from 'react-live';
import type {LiveProviderProps} from 'react-live';
import {Box} from '@twilio-paste/box';
import {Button} from '@twilio-paste/button';
import {Theme, useTheme} from '@twilio-paste/theme';
Expand All @@ -10,15 +11,14 @@ import {HideIcon} from '@twilio-paste/icons/esm/HideIcon';

import {CodeblockTheme} from './theme';
import {CodeBlockOverlayShadow} from './CodeBlockOverlayShadow';
import type {Language} from '../../codeblock';
import {CopyButton} from '../../CopyButton';
import {ThemeSwitcher} from './ThemeSwitcher';
import {usePreviewThemeContext} from '../../../context/PreviewThemeContext';

interface LivePreviewProps {
children: string;
scope: {[key: string]: any};
language?: Language;
language?: LiveProviderProps['language'];
disabled?: boolean;
noInline?: boolean;
showOverflow?: boolean;
Expand Down

0 comments on commit 0d57f59

Please sign in to comment.