Skip to content
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

LG-2044: Code base font size #2671

Open
wants to merge 6 commits into
base: LG-4657-code-improvements-integration
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .changeset/chatty-ears-exercise.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ e.g.
</Code>
```

### `baseFontSize`
Adds `baseFontSize` prop, which allows you to override the `LeafyGreenProvider` value.

## What's changed?

The `className` prop is no longer applied to the `<pre>` tag. Instead it is applied to the parent `<div>` wrapper.
Expand Down
1 change: 1 addition & 0 deletions packages/code/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ const SomeComponentWithoutPanel = () => (
| `copyButtonAppearance` | `'none'`, `'hover'`, `'persist'` | Determines the appearance of the copy button without a panel. The copy button allows the code block to be copied to the user's clipboard by clicking the button. If `hover`, the copy button will only appear when the user hovers over the code block. On mobile devices, the copy button will always be visible. If `persist`, the copy button will always be visible. If `none`, the copy button will not be rendered. **_Note: `panel` cannot be used with `copyButtonAppearance`. Either use `copyButtonAppearance` or `panel`, not both_**. | `hover` |
| `panel` | `React.ReactNode` | Slot to pass the `<Panel/>` sub-component which will render the top panel with a language switcher, custom action buttons, and copy button. If no props are passed to the panel sub-component, the panel will render with only the copy button. **_Note: `copyButtonAppearance` cannot be used with `panel`. Either use `copyButtonAppearance` or `panel`, not both._** | `` |
| `isLoading` | `boolean` | Determines whether or not the loading skeleton will be rendered in place of the code block. If`true`, the language switcher and copy button will be disabled in the top panel. | `false` |
| `baseFontSize` | `'13'` \| `'16'` | Determines the base font-size of the component | `13` |
| `copyable` (`Deprecated`) | `boolean` | When true, allows the code block to be copied to the user's clipboard. **_Note:_** `@deprecated` - use `<Panel />` or `copyButtonAppearance` | `false` |
| `chromeTitle`(`Deprecated`) | `string` | Shows a filename-like title in the window chrome frame.**NOTE:** While you can set this prop if `showWindowChrome` is `false`, it will not be displayed unless the `showWindowChrome` prop is `true`. **_Note:_** `@deprecated` - use `panel={<Panel title={} />}` | `''` |
| `showCustomActionButtons` (`Deprecated`) | `boolean` | Shows custom action buttons in the panel if set to `true` and there is at least one item in `customActionButtons`. **_Note:_** `@deprecated` - use `<Panel showCustomActionButtons={} />` | `false` |
Expand Down
99 changes: 43 additions & 56 deletions packages/code/src/Code.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
import { css } from '@leafygreen-ui/emotion';
import Icon from '@leafygreen-ui/icon';
import IconButton from '@leafygreen-ui/icon-button';
import LeafygreenProvider from '@leafygreen-ui/leafygreen-provider';

import {
languageOptions,
Expand Down Expand Up @@ -126,21 +125,18 @@ interface FontSizeProps {
}

export const LiveExample: StoryType<typeof Code, FontSizeProps> = ({
baseFontSize,
highlightLines,
...args
}: CodeProps & FontSizeProps) => (
<LeafygreenProvider baseFontSize={baseFontSize}>
<Code
{...(args as CodeProps)}
highlightLines={highlightLines ? [6, [10, 15]] : undefined}
className={css`
width: 100%;
`}
>
{jsSnippet}
</Code>
</LeafygreenProvider>
<Code
{...(args as CodeProps)}
highlightLines={highlightLines ? [6, [10, 15]] : undefined}
className={css`
width: 100%;
`}
>
{jsSnippet}
</Code>
);
LiveExample.parameters = {
chromatic: {
Expand All @@ -149,38 +145,32 @@ LiveExample.parameters = {
};

export const WithCustomActions: StoryType<typeof Code, FontSizeProps> = ({
baseFontSize,
highlightLines,
...args
}: CodeProps & FontSizeProps) => (
<LeafygreenProvider baseFontSize={baseFontSize}>
<Code
{...(args as CodeProps)}
copyButtonAppearance={undefined}
highlightLines={highlightLines ? [6, [10, 15]] : undefined}
panel={
<Panel
customActionButtons={customActionButtons}
showCustomActionButtons
/>
}
>
{jsSnippet}
</Code>
</LeafygreenProvider>
<Code
{...(args as CodeProps)}
copyButtonAppearance={undefined}
highlightLines={highlightLines ? [6, [10, 15]] : undefined}
panel={
<Panel
customActionButtons={customActionButtons}
showCustomActionButtons
/>
}
>
{jsSnippet}
</Code>
);

export const WithLanguageSwitcher: StoryType<typeof Code, FontSizeProps> = ({
baseFontSize,
...args
}: CodeProps & FontSizeProps) => (
<LeafygreenProvider baseFontSize={baseFontSize}>
<LanguageSwitcherWithPanelExample
showCustomActionButtons={true}
customActionButtons={customActionButtons}
{...args}
/>
</LeafygreenProvider>
<LanguageSwitcherWithPanelExample
showCustomActionButtons={true}
customActionButtons={customActionButtons}
{...args}
/>
);
WithLanguageSwitcher.parameters = {
controls: {
Expand All @@ -198,17 +188,15 @@ WithLanguageSwitcher.parameters = {
export const WithDeprecatedCustomActionProps: StoryType<
typeof Code,
FontSizeProps
> = ({ baseFontSize, highlightLines, ...args }: CodeProps & FontSizeProps) => (
<LeafygreenProvider baseFontSize={baseFontSize}>
<Code
{...(args as CodeProps)}
highlightLines={highlightLines ? [6, [10, 15]] : undefined}
customActionButtons={customActionButtons}
showCustomActionButtons
>
{jsSnippet}
</Code>
</LeafygreenProvider>
> = ({ highlightLines, ...args }: CodeProps) => (
<Code
{...(args as CodeProps)}
highlightLines={highlightLines ? [6, [10, 15]] : undefined}
customActionButtons={customActionButtons}
showCustomActionButtons
>
{jsSnippet}
</Code>
);
WithDeprecatedCustomActionProps.parameters = {
controls: {
Expand All @@ -226,14 +214,12 @@ WithDeprecatedCustomActionProps.parameters = {
export const WithDeprecatedLanguageSwitcherProps: StoryType<
typeof Code,
FontSizeProps
> = ({ baseFontSize, ...args }: CodeProps & FontSizeProps) => (
<LeafygreenProvider baseFontSize={baseFontSize}>
<LanguageSwitcherWithDeprecatedPropsExample
showCustomActionButtons={true}
customActionButtons={customActionButtons}
{...args}
/>
</LeafygreenProvider>
> = ({ ...args }: CodeProps) => (
<LanguageSwitcherWithDeprecatedPropsExample
showCustomActionButtons={true}
customActionButtons={customActionButtons}
{...args}
/>
);
WithDeprecatedLanguageSwitcherProps.parameters = {
controls: {
Expand Down Expand Up @@ -326,6 +312,7 @@ WithoutPanel.parameters = {

export const Loading = () => {};
Loading.parameters = {
chromatic: { delay: 5000 }, // 5-second delay
controls: {
exclude: /.*/g,
},
Expand Down
5 changes: 3 additions & 2 deletions packages/code/src/Code/Code.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { useIsomorphicLayoutEffect } from '@leafygreen-ui/hooks';
import ChevronDown from '@leafygreen-ui/icon/dist/ChevronDown';
import ChevronUp from '@leafygreen-ui/icon/dist/ChevronUp';
import { useDarkMode } from '@leafygreen-ui/leafygreen-provider';
import { useBaseFontSize } from '@leafygreen-ui/leafygreen-provider';
import { CodeSkeleton } from '@leafygreen-ui/skeleton-loader';
import { useUpdatedBaseFontSize } from '@leafygreen-ui/typography';

import CodeContextProvider from '../CodeContext/CodeContext';
import { LGIDs, numOfCollapsedLinesOfCode } from '../constants';
Expand Down Expand Up @@ -54,6 +54,7 @@ function Code({
className,
onCopy,
panel,
baseFontSize: baseFontSizeProp,
// Deprecated props
copyable = false,
showCustomActionButtons = false,
Expand All @@ -72,7 +73,7 @@ function Code({
const [collapsedCodeHeight, setCollapsedCodeHeight] = useState<number>(0);
const isMultiline = useMemo(() => hasMultipleLines(children), [children]);
const { theme, darkMode } = useDarkMode(darkModeProp);
const baseFontSize = useBaseFontSize();
const baseFontSize = useUpdatedBaseFontSize(baseFontSizeProp);

useIsomorphicLayoutEffect(() => {
const scrollableElement = scrollableElementRef.current;
Expand Down
9 changes: 9 additions & 0 deletions packages/code/src/Code/Code.types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { BaseFontSize } from '@leafygreen-ui/tokens';

import { LanguageOption } from '../Panel/Panel.types';
import { SyntaxProps } from '../Syntax/Syntax.types';
import { Language } from '../types';
Expand Down Expand Up @@ -111,6 +113,13 @@ export type CodeProps = Omit<
* @deprecated
*/
copyable?: boolean;

/**
* Determines the base font-size of the component
*
* @default 13
*/
baseFontSize?: BaseFontSize;
} & (
| {
/**
Expand Down