From 1c0fa2c6249f7845715a1d5031e45483420577e5 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Fri, 23 Aug 2019 14:11:46 +0300 Subject: [PATCH] Cards with browser support (WIP) --- src/docs/components/SnippetCard.js | 35 ++++++++++++++---------------- src/docs/styles/_card.scss | 25 ++++++++++++++++++++- src/docs/templates/SnippetPage.js | 4 ++++ src/docs/util/index.js | 32 +++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 20 deletions(-) diff --git a/src/docs/components/SnippetCard.js b/src/docs/components/SnippetCard.js index 05adc049f..8c9d52e60 100644 --- a/src/docs/components/SnippetCard.js +++ b/src/docs/components/SnippetCard.js @@ -2,7 +2,7 @@ import React from 'react'; import { CopyToClipboard } from 'react-copy-to-clipboard'; import config from '../../../config'; -import { getTextualContent, getCodeBlocks, optimizeAllNodes } from '../util'; +import { getTextualContent, getCodeBlocks, optimizeAllNodes, getExplanation, getBrowserSupport } from '../util'; // import ShareIcon from './SVGs/ShareIcon'; import AniLink from 'gatsby-plugin-transition-link/AniLink'; import CollapseOpenIcon from './SVGs/CollapseOpenIcon'; @@ -76,24 +76,21 @@ const FullCard = ({ snippetData, isDarkMode }) => {
- {/* - - {examplesOpen && ( -
-          )}
-         */}
+        
Explanation
+
+
Browser support
+

{snippetData.supportPercentage}%

+
); diff --git a/src/docs/styles/_card.scss b/src/docs/styles/_card.scss index 947e29947..cf3042ee4 100644 --- a/src/docs/styles/_card.scss +++ b/src/docs/styles/_card.scss @@ -100,7 +100,7 @@ } } -.card-section-demo-title { +.card-section-demo-title, .card-section-explanation-title,.card-section-browser-support-title { margin: 0.5rem 1rem .5rem; color: var(--card-fore-color-light); font-size: 1rem; @@ -108,6 +108,29 @@ line-height: 2.25; } +.card-explanation, .card-browser-support { + margin: 0.5rem; + p, li { + line-height: 1.75; + margin-bottom: 0.5rem; + } + position: relative; + width: calc( 100% + 3rem); + display: block; +} + +.card-browser-support { + margin: 1rem; +} + +.browser-support-data { + font-size: 1.75rem; + font-weight: 300; + color: var(--card-fore-color-light); + line-height: 1; + margin: 0.5rem 1rem .5rem; +} + .card-snippet-demo { width: calc(100% - 0.25rem); margin: 0.5rem 1rem .5rem; diff --git a/src/docs/templates/SnippetPage.js b/src/docs/templates/SnippetPage.js index dbb6ef8ae..239497431 100644 --- a/src/docs/templates/SnippetPage.js +++ b/src/docs/templates/SnippetPage.js @@ -38,6 +38,7 @@ const SnippetPage = props => { html: post.html, code: postData.attributes.codeBlocks, tags: postData.attributes.tags, + supportPercentage: postData.attributes.browserSupport.supportPercentage }} isDarkMode={props.isDarkMode} /> @@ -102,6 +103,9 @@ export const pageQuery = graphql` js scopedCss } + browserSupport { + supportPercentage + } tags } } diff --git a/src/docs/util/index.js b/src/docs/util/index.js index 6ce5afaa9..95821255a 100644 --- a/src/docs/util/index.js +++ b/src/docs/util/index.js @@ -22,6 +22,36 @@ const getTextualContent = (str, noExplain = false) => { return results[1]; }; +// Gets the explanation for a snippet file. +const getExplanation = str => { + const regex = /

\s*Explanation\s*<\/h4>([\s\S]*)

/g; + const results = []; + let m = null; + while ((m = regex.exec(str)) !== null) { + if (m.index === regex.lastIndex) regex.lastIndex += 1; + + m.forEach((match, groupIndex) => { + results.push(match); + }); + } + return results[1].replace(/\r\n/g, '\n'); +}; + +// Gets the browser support for a snippet file. +const getBrowserSupport = str => { + const regex = /

\s*Browser [s|S]upport\s*<\/h4>([\s\S]*)/g; + const results = []; + let m = null; + while ((m = regex.exec(str)) !== null) { + if (m.index === regex.lastIndex) regex.lastIndex += 1; + + m.forEach((match, groupIndex) => { + results.push(match); + }); + } + return results[1].replace(/\r\n/g, '\n'); +}; + // Gets the code blocks in a gatsby page const getCodeBlocks = str => { const regex = //g; @@ -131,5 +161,7 @@ module.exports = { getCodeBlocks, optimizeNodes, optimizeAllNodes, + getExplanation, getRawCodeBlocks, + getBrowserSupport };