From 71ea8754560bcb2f2ef6b36b28f43edea3b156b4 Mon Sep 17 00:00:00 2001 From: Kyle Suss Date: Wed, 25 Sep 2019 16:38:22 -0600 Subject: [PATCH] Build out Highlight component to auto format, consolidate global code styling --- src/components/Highlight.js | 53 +++++++++----- src/components/Highlight.stories.js | 107 +++++++++++++++++++++------- src/components/shared/global.js | 28 -------- 3 files changed, 117 insertions(+), 71 deletions(-) diff --git a/src/components/Highlight.js b/src/components/Highlight.js index 0134094a..1f3a1f62 100644 --- a/src/components/Highlight.js +++ b/src/components/Highlight.js @@ -7,7 +7,8 @@ import loadLanguages from 'prismjs/components/index'; import PropTypes from 'prop-types'; import { color, typography } from './shared/styles'; -loadLanguages(['bash', 'typescript', 'json']); +const languages = ['bash', 'javascript', 'typescript', 'json']; +loadLanguages(languages); // Prism theme copied from 'prismjs/themes/prism.css.' -- without Webpack, the CSS // cannot be imported easily and any app which pulls in the design system will @@ -15,26 +16,32 @@ loadLanguages(['bash', 'typescript', 'json']); // the theme over. // prettier-ignore const HighlightBlock = styled.div` - /* Theme below from: prismjs/themes/prism.css */ - code[class*=language-],pre[class*=language-]{color:#000;background:0 0;text-shadow:0 1px #fff;font-family:Consolas,Monaco,'Andale Mono','Ubuntu Mono',monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none}code[class*=language-] ::-moz-selection,code[class*=language-]::-moz-selection,pre[class*=language-] ::-moz-selection,pre[class*=language-]::-moz-selection{text-shadow:none;background:#b3d4fc}code[class*=language-] ::selection,code[class*=language-]::selection,pre[class*=language-] ::selection,pre[class*=language-]::selection{text-shadow:none;background:#b3d4fc}@media print{code[class*=language-],pre[class*=language-]{text-shadow:none}}pre[class*=language-]{padding:1em;margin:.5em 0;overflow:auto}:not(pre)>code[class*=language-],pre[class*=language-]{background:#f5f2f0}:not(pre)>code[class*=language-]{padding:.1em;border-radius:.3em;white-space:normal}.token.cdata,.token.comment,.token.doctype,.token.prolog{color:#708090}.token.punctuation{color:#999}.namespace{opacity:.7}.token.boolean,.token.constant,.token.deleted,.token.number,.token.property,.token.symbol,.token.tag{color:#905}.token.attr-name,.token.builtin,.token.char,.token.inserted,.token.selector,.token.string{color:#690}.language-css .token.string,.style .token.string,.token.entity,.token.operator,.token.url{color:#a67f59;background:hsla(0,0%,100%,.5)}.token.atrule,.token.attr-value,.token.keyword{color:#07a}.token.class-name,.token.function{color:#dd4a68}.token.important,.token.regex,.token.variable{color:#e90}.token.bold,.token.important{font-weight:700}.token.italic{font-style:italic}.token.entity{cursor:help} + /* + Line below from: prismjs/themes/prism.css, with removal of overly specific + selectors (primarily [class*=language-]) so they are easier to override. + */ + code,pre{color:#000;background:0 0;text-shadow:0 1px #fff;font-family:Consolas,Monaco,'Andale Mono','Ubuntu Mono',monospace;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none}code ::-moz-selection,code::-moz-selection,pre ::-moz-selection,pre::-moz-selection{text-shadow:none;background:#b3d4fc}code ::selection,code::selection,pre ::selection,pre::selection{text-shadow:none;background:#b3d4fc}@media print{code,pre{text-shadow:none}}pre{padding:1em;margin:.5em 0;overflow:auto}:not(pre)>code,pre{background:#f5f2f0}:not(pre)>code{padding:.1em;border-radius:.3em;white-space:normal}.token.cdata,.token.comment,.token.doctype,.token.prolog{color:#708090}.token.punctuation{color:#999}.namespace{opacity:.7}.token.boolean,.token.constant,.token.deleted,.token.number,.token.property,.token.symbol,.token.tag{color:#905}.token.attr-name,.token.builtin,.token.char,.token.inserted,.token.selector,.token.string{color:#690}.language-css .token.string,.style .token.string,.token.entity,.token.operator,.token.url{color:#a67f59;background:hsla(0,0%,100%,.5)}.token.atrule,.token.attr-value,.token.keyword{color:#07a}.token.class-name,.token.function{color:#dd4a68}.token.important,.token.regex,.token.variable{color:#e90}.token.bold,.token.important{font-weight:700}.token.italic{font-style:italic}.token.entity{cursor:help} - *:not(pre) > code[class*='language-'], - pre[class*='language-'] { - background: ${color.lighter}; - margin: 2em 0; - } - - code[class*='language-'], - pre[class*='language-'] { - font-size: ${typography.size.s3}px; + code, + pre { + font-family: ${typography.type.code}; + font-size: ${typography.size.s2 - 1}px; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; } code { - font-size: 17px; + display: inline-block; + vertical-align: baseline; } - .aside code { - font-size: 15px; + pre { + line-height: 18px; + padding: 11px 1rem; + white-space: pre-wrap; + background: ${color.lighter}; + border-radius: 3px; + margin: 1rem 0; } `; @@ -53,11 +60,18 @@ export class Highlight extends React.Component { } render() { - const { children, ...rest } = this.props; + const { children, language, ...rest } = this.props; + const codeBlock =
; return ( -
+ {language ? ( +
+            {codeBlock}
+          
+ ) : ( + codeBlock + )} ); } @@ -65,4 +79,9 @@ export class Highlight extends React.Component { Highlight.propTypes = { children: PropTypes.node.isRequired, + language: PropTypes.oneOf(languages), +}; + +Highlight.defaultProps = { + language: null, }; diff --git a/src/components/Highlight.stories.js b/src/components/Highlight.stories.js index 72d74a4a..31109bc1 100644 --- a/src/components/Highlight.stories.js +++ b/src/components/Highlight.stories.js @@ -1,15 +1,14 @@ import React from 'react'; +import styled from 'styled-components'; import { Highlight } from './Highlight'; -const bash = ` -
# Highlight bash:
+const bashCode = `# Highlight bash:
 npx install some-package-name
-cd some-package-name
-
-`; +cd some-package-name`; + +const bashCodeWithWrappers = `
${bashCode}
`; -const javascript = ` -
// Highlight JavaScript:
+const javascriptCode = `// Highlight JavaScript:
 import React from 'react'
 
 const MyComponent = () => (
@@ -17,11 +16,11 @@ const MyComponent = () => (
 )
 
 export default MyComponent
-
`; -const typescript = ` -
// Highlight Typescript:
+const javascriptCodeWithWrappers = `
${javascriptCode}
`; + +const typescriptCode = `// Highlight Typescript: import React from 'react' interface InterfaceMyComponentProps { @@ -33,19 +32,19 @@ const MyComponent: React.SFC<InterfaceMyComponentProps> = ({ isCool }) => ( ) export default MyComponent -
`; -const css = ` -
/* Highlight CSS: */
+const typescriptCodeWithWrappers = `
${typescriptCode}
`; + +const cssCode = `/* Highlight CSS: */ .someClass { position: relative; } -
`; -const json = ` -
{
+const cssCodeWithWrappers = `
${cssCode}
`; + +const jsonCode = `{ "name": "@yourorg/package", "version": "0.0.1", "description": "A sweet package", @@ -53,9 +52,9 @@ const json = ` "type": "git", "url": "https://github.com/yourorg/package.git" } -} -
-`; +}`; + +const jsonCodeWithWrappers = `
${jsonCode}
`; export default { title: 'Design System|Highlight', @@ -63,15 +62,71 @@ export default { }; export const allLanguages = () => ( -
- {bash} - {javascript} - {typescript} - {css} - {json} -
+ + {[ + bashCodeWithWrappers, + javascriptCodeWithWrappers, + typescriptCodeWithWrappers, + cssCodeWithWrappers, + jsonCodeWithWrappers, + ].join('')} + ); allLanguages.story = { name: 'all languages', }; + +export const bash = () => ( + <> + Autoformat + {bashCode} + Pre-formatted + {bashCodeWithWrappers} + +); + +export const javascript = () => ( + <> + Autoformat + {javascriptCode} + Pre-formatted + {javascriptCodeWithWrappers} + +); + +export const typescript = () => ( + <> + Autoformat + {typescriptCode} + Pre-formatted + {typescriptCodeWithWrappers} + +); + +export const css = () => ( + <> + Autoformat + {cssCode} + Pre-formatted + {cssCodeWithWrappers} + +); + +export const json = () => ( + <> + Autoformat + {jsonCode} + Pre-formatted + {jsonCodeWithWrappers} + +); + +const StyledHighlight = styled(Highlight)` + code, + pre { + font-size: 16px; + } +`; + +export const customStyling = () => {jsonCode}; diff --git a/src/components/shared/global.js b/src/components/shared/global.js index 5a10520b..3512a772 100644 --- a/src/components/shared/global.js +++ b/src/components/shared/global.js @@ -64,34 +64,6 @@ export const bodyStyles = css` margin-bottom: 1.25rem; } - code, - pre { - font-family: ${typography.type.code}; - font-size: ${typography.size.s2 - 1}px; - -webkit-font-smoothing: antialiased; - -moz-osx-font-smoothing: grayscale; - } - - code { - display: inline-block; - padding-left: 2px; - padding-right: 2px; - vertical-align: baseline; - - color: ${color.secondary}; - } - - pre { - line-height: 18px; - padding: 11px 1rem; - white-space: pre-wrap; - - background: rgba(0, 0, 0, 0.05); - color: ${color.darkest}; - border-radius: 3px; - margin: 1rem 0; - } - &.ReactModal__Body--open { overflow: hidden; &.hide-intercom #intercom-container {