From a8d6fbac01b6917d0f843c09cf6a8db371d851ba Mon Sep 17 00:00:00 2001 From: Michelle Tilley Date: Mon, 16 Dec 2019 14:09:08 -0800 Subject: [PATCH] Finish up Variables page --- docs/content/support/variables.mdx | 7 +- docs/gatsby-node.js | 12 +++ docs/src/variables.js | 157 +++++++++++++++++++---------- 3 files changed, 121 insertions(+), 55 deletions(-) create mode 100644 docs/gatsby-node.js diff --git a/docs/content/support/variables.mdx b/docs/content/support/variables.mdx index 11bc3ad0..6c4f97cc 100644 --- a/docs/content/support/variables.mdx +++ b/docs/content/support/variables.mdx @@ -1,11 +1,16 @@ --- title: Variables +path: support/variables +status: Experimental +bundle: alerts --- import {Variables, DeprecationIcon} from '../../src/variables' + + The tables below list all of the global SCSS variables defined in [the `support/variables` directory](https://github.com/primer/css/tree/master/src/support/variables). Variables with a are planned for [deprecation](../tools/deprecations) in a future version of `@primer/css`, and should be avoided. - + diff --git a/docs/gatsby-node.js b/docs/gatsby-node.js new file mode 100644 index 00000000..37b8ebba --- /dev/null +++ b/docs/gatsby-node.js @@ -0,0 +1,12 @@ +exports.onCreateWebpackConfig = ({actions, stage, plugins}) => { + actions.setWebpackConfig({ + node: { + fs: 'empty' + }, + plugins: [ + plugins.define({ + __DEV__: stage === 'develop' || stage === 'develop-html' + }) + ] + }) +} diff --git a/docs/src/variables.js b/docs/src/variables.js index d9edf7d9..cba060e1 100644 --- a/docs/src/variables.js +++ b/docs/src/variables.js @@ -1,11 +1,10 @@ import React from 'react' +import mdx from '@mdx-js/mdx' import {Box, Flex, Link, Text, Tooltip} from '@primer/components' import Octicon, {Alert} from '@primer/octicons-react' import themeGet from '@styled-system/theme-get' import DoctocatTable from '@primer/gatsby-theme-doctocat/src/components/table' import styled from 'styled-components' -import variables from '../dist/variables.json' -import deprecations from '../dist/deprecations.json' const Table = styled(DoctocatTable)` font-size: ${themeGet('fontSizes.1')}px; @@ -24,37 +23,82 @@ const Table = styled(DoctocatTable)` } ` -for (const name of Object.keys(variables)) { - if (name.endsWith('-font')) { - delete variables[name] - } +function useVariables() { + return React.useMemo(() => { + let variablesByFile = new Map() + + try { + const variables = require('../dist/variables.json') + const deprecations = useDeprecations() + + for (const name of Object.keys(variables)) { + if (name.endsWith('-font')) { + delete variables[name] + } + } + + variablesByFile = Object.entries(variables).reduce((map, [name, variable]) => { + const { + source: {path} + } = variable + + variable.name = name + variable.deprecated = deprecations.variables.hasOwnProperty(name) + + if (map.has(path)) { + map.get(path).push(variable) + } else { + map.set(path, [variable]) + } + + return map + }, variablesByFile) + + for (const variables of variablesByFile.values()) { + variables.sort((a, b) => { + return a.deprecated - b.deprecated || a.source.line - b.source.line + }) + } + + return variablesByFile + } catch (err) { + return new Map() + } + }, []) } -const variablesByFile = Object.entries(variables).reduce((map, [name, variable]) => { - const { - source: {path} - } = variable - - variable.name = name - variable.deprecated = deprecations.variables.hasOwnProperty(name) - - if (map.has(path)) { - map.get(path).push(variable) - } else { - map.set(path, [variable]) - } - return map -}, new Map()) - -for (const [path, variables] of variablesByFile.entries()) { - variables.sort((a, b) => { - return a.deprecated - b.deprecated || a.source.line - b.source.line - }) +function useDeprecations() { + return React.useMemo(() => { + try { + const deprecations = require('../dist/deprecations.json') + return deprecations + } catch (err) { + return {} + } + }, []) } -function Variables(props) { +function Variables({children, ...props}) { + const variablesByFile = useVariables() + console.log(__DEV__) + + if (variablesByFile.size === 0) { + return ( +
+

No data available

+ {__DEV__ && ( +

+ This probably means that the root project has not been built; run `npm run dist` and restart your + development server. +

+ )} +
+ ) + } + return Array.from(variablesByFile.entries()).map(([path, variables]) => ( <> + {children}

Defined in {path}

@@ -67,33 +111,32 @@ function Variables(props) { - {variables - .map(({name, computed, values, source, refs}) => ( - - - - - # + {variables.map(({name, computed, values, source, refs}) => ( + + + + + # + + + + {name} - - - {name} - - - - - - - - - - {computed} - - - - - - ))} + + + + + + + + + {computed} + + + + + + ))} @@ -133,6 +176,8 @@ function RefList({refs}) { } function DeprecationFlag({variable, ...rest}) { + const deprecations = useDeprecations() + const dep = deprecations.variables[variable] return dep ? ( @@ -144,7 +189,11 @@ function DeprecationFlag({variable, ...rest}) { } function DeprecationIcon(props) { - return + return ( + + + + ) } export {Variables, DeprecationIcon}