From 9c7674de9885ab7d194419bb2e5c21407e93978c Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Wed, 3 Jun 2020 17:40:02 -0700 Subject: [PATCH] feat: Add a FunctionDefinition component and render the function def for a method --- src/components/FunctionDefinition.js | 54 +++++++++++++++++++ src/components/FunctionDefinition.module.scss | 35 ++++++++++++ src/components/styles.scss | 11 ++++ src/hooks/useComponentDoc.js | 2 +- src/templates/ReferenceTemplate.js | 12 ++++- 5 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 src/components/FunctionDefinition.js create mode 100644 src/components/FunctionDefinition.module.scss diff --git a/src/components/FunctionDefinition.js b/src/components/FunctionDefinition.js new file mode 100644 index 000000000..e5ef1d7ef --- /dev/null +++ b/src/components/FunctionDefinition.js @@ -0,0 +1,54 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import Markdown from 'react-markdown'; +import styles from './FunctionDefinition.module.scss'; + +const ParamDescription = ({ children, ...props }) => ( + + // {children} + +); + +ParamDescription.propTypes = { + children: PropTypes.node, +}; + +const FunctionDefinition = ({ params, returnValue }) => { + return ( +
+ + {params.length > 0 ? 'function (' : 'function ()'} + + {params.map((param, i) => ( +
+ {param.name}: + {param.type} + +
+ ))} + {params.length > 0 && ) } + => + {returnValue.type} +
+ ); +}; + +FunctionDefinition.propTypes = { + params: PropTypes.arrayOf( + PropTypes.shape({ + name: PropTypes.string, + type: PropTypes.string, + description: PropTypes.string, + }) + ), + returnValue: PropTypes.shape({ + type: PropTypes.string.isRequired, + }), +}; + +export default FunctionDefinition; diff --git a/src/components/FunctionDefinition.module.scss b/src/components/FunctionDefinition.module.scss new file mode 100644 index 000000000..77a55047d --- /dev/null +++ b/src/components/FunctionDefinition.module.scss @@ -0,0 +1,35 @@ +.container { + font-family: 'Menlo', 'Consolas', monospace; + font-size: 0.875rem; + line-height: 1.5; +} + +.line:not(:last-child) { + margin-bottom: 0.5rem; +} + +.param { + margin-left: 1rem; +} + +.paramName { + color: var(--color-neutrals-700); +} + +.paramDescription { + color: var(--color-neutrals-500); + + > p { + color: inherit; + display: inline; + } +} + +.keyword { + color: var(--color-neutrals-600); +} + +.type { + color: var(--color-green-6); + background: var(--color-green-1); +} diff --git a/src/components/styles.scss b/src/components/styles.scss index 761195d09..89b4365dd 100644 --- a/src/components/styles.scss +++ b/src/components/styles.scss @@ -35,6 +35,17 @@ --color-red-800: #390000; --color-red-900: #1b0000; + --color-green-1: #f1fdf2; + --color-green-2: #d1f7d9; + --color-green-3: #7fe898; + --color-green-4: #22e749; + --color-green-5: #00d100; + --color-green-6: #00a500; + --color-green-7: #008200; + --color-green-8: #004d00; + --color-green-9: #0d290a; + --color-green-10: #021200; + --boxshadow: 0px 2.76726px 2.21381px rgba(0, 85, 90, 0.0168687), 0px 6.6501px 5.32008px rgba(0, 85, 90, 0.0242336), 0px 12.5216px 10.0172px rgba(0, 85, 90, 0.03), diff --git a/src/hooks/useComponentDoc.js b/src/hooks/useComponentDoc.js index 07907e50d..3e39bdfc7 100644 --- a/src/hooks/useComponentDoc.js +++ b/src/hooks/useComponentDoc.js @@ -37,7 +37,7 @@ const useComponentDoc = (componentName) => { return { name: `${componentName}.${member}`, description: methodDocs?.text, - returnValue: methodDocs?.tags.return, + returnValue: methodDocs?.tags.return?.[0] ?? { type: 'undefined' }, params: methodDocs?.tags.param, }; }), diff --git a/src/templates/ReferenceTemplate.js b/src/templates/ReferenceTemplate.js index 7b9b9500d..ffd9fa578 100644 --- a/src/templates/ReferenceTemplate.js +++ b/src/templates/ReferenceTemplate.js @@ -1,10 +1,11 @@ -import React, { useState } from 'react'; +import React, { Fragment, useState } from 'react'; import cx from 'classnames'; import { graphql } from 'gatsby'; import PropTypes from 'prop-types'; import ReactMarkdown from 'react-markdown'; import Container from '../components/Container'; import ComponentExample from '../components/ComponentExample'; +import FunctionDefinition from '../components/FunctionDefinition'; import Layout from '../components/Layout'; import Sidebar from '../components/Sidebar'; import SEO from '../components/Seo'; @@ -65,7 +66,14 @@ const ReferenceTemplate = ({ data }) => {

Methods

{methods.map((method, i) => ( -

{method.name}

+ +

{method.name}

+ + +
))}
)}