Cards with browser support (WIP)

This commit is contained in:
Angelos Chalaris
2019-08-23 14:11:46 +03:00
parent d12e35a5c8
commit 1c0fa2c624
4 changed files with 76 additions and 20 deletions

View File

@ -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 }) => {
</style>
<div dangerouslySetInnerHTML={{__html: snippetData.code.html}} />
</div>
{/* <button
className='button button-example-toggler'
onClick={() => setExamplesOpen(!examplesOpen)}
>
{examplesOpen ? <CollapseOpenIcon /> : <CollapseClosedIcon />}Examples
</button>
<ReactCSSTransitionReplace
transitionName='roll-up'
transitionEnterTimeout={300}
transitionLeaveTimeout={300}
>
{examplesOpen && (
<pre
className={`section card-examples language-${config.language}`}
dangerouslySetInnerHTML={{ __html: cardExamplesHtml }}
/>
)}
</ReactCSSTransitionReplace> */}
<h5 className='card-section-explanation-title'>Explanation</h5>
<div
className='card-explanation'
dangerouslySetInnerHTML={{
__html: `${getExplanation(snippetData.html)}`,
}}
/>
<h5 className='card-section-browser-support-title'>Browser support</h5>
<p className='browser-support-data'>{snippetData.supportPercentage}%</p>
<div
className='card-browser-support'
dangerouslySetInnerHTML={{
__html: `${getBrowserSupport(snippetData.html)}`,
}}
/>
</div>
</div>
);

View File

@ -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;

View File

@ -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
}
}

View File

@ -22,6 +22,36 @@ const getTextualContent = (str, noExplain = false) => {
return results[1];
};
// Gets the explanation for a snippet file.
const getExplanation = str => {
const regex = /<h4>\s*Explanation\s*<\/h4>([\s\S]*)<h4>/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 = /<h4>\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 = /<pre[.\S\s]*?<\/pre>/g;
@ -131,5 +161,7 @@ module.exports = {
getCodeBlocks,
optimizeNodes,
optimizeAllNodes,
getExplanation,
getRawCodeBlocks,
getBrowserSupport
};