From 37825431e1f4ecc5534a2d1dac6aefdf141e4ab7 Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Wed, 14 Oct 2020 23:29:17 -0700 Subject: [PATCH] feat: show cursor on terminal prompts --- src/components/Terminal/CommandLine.js | 6 +++++- src/components/Terminal/Cursor.js | 1 + src/components/Terminal/Shell.js | 27 ++++++++++++++++++-------- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/components/Terminal/CommandLine.js b/src/components/Terminal/CommandLine.js index 0ea2acc90..54ce0d017 100644 --- a/src/components/Terminal/CommandLine.js +++ b/src/components/Terminal/CommandLine.js @@ -2,13 +2,15 @@ import React from 'react'; import PropTypes from 'prop-types'; import Prompt from './Prompt'; import { css } from '@emotion/core'; +import Cursor from './Cursor'; -const CommandLine = ({ line, prompt, getTokenProps }) => ( +const CommandLine = ({ cursor, line, prompt, getTokenProps }) => (
@@ -22,11 +24,13 @@ const CommandLine = ({ line, prompt, getTokenProps }) => ( // eslint-disable-next-line react/jsx-key ))} + {cursor && }
); CommandLine.propTypes = { + cursor: PropTypes.bool, line: PropTypes.arrayOf(PropTypes.object).isRequired, getTokenProps: PropTypes.func.isRequired, prompt: PropTypes.oneOf(['$', '>']), diff --git a/src/components/Terminal/Cursor.js b/src/components/Terminal/Cursor.js index 025a2316e..9e35620f2 100644 --- a/src/components/Terminal/Cursor.js +++ b/src/components/Terminal/Cursor.js @@ -18,6 +18,7 @@ const Cursor = () => ( width: 1ch; height: 1.25em; animation: ${blink} 1s infinite; + vertical-align: middle; `} /> ); diff --git a/src/components/Terminal/Shell.js b/src/components/Terminal/Shell.js index 1f2e14864..2efead0f9 100644 --- a/src/components/Terminal/Shell.js +++ b/src/components/Terminal/Shell.js @@ -1,11 +1,22 @@ -import React from 'react'; +import React, { useState, useEffect } from 'react'; import PropTypes from 'prop-types'; import { css } from '@emotion/core'; import Command from './Command'; +import CommandLine from './CommandLine'; import theme from './theme'; import rollupIntoCommands from './rollupIntoCommands'; +import { useTimeout } from '@newrelic/gatsby-theme-newrelic'; const Shell = ({ highlight, code }) => { + const [step, setStep] = useState(0); + const { tokens, getTokenProps } = highlight; + const commands = rollupIntoCommands(tokens, code); + const shownCommands = commands.slice(0, step); + + useTimeout(() => { + setStep((step) => step + 1); + }, 3000); + return (
 {
       `}
     >
       
-        {rollupIntoCommands(highlight.tokens, code).map((command, idx) => (
-          
+        {shownCommands.map((command, idx) => (
+          
         ))}
+        
       
     
); @@ -54,7 +62,10 @@ const Shell = ({ highlight, code }) => { Shell.propTypes = { code: PropTypes.string.isRequired, - highlight: PropTypes.object, + highlight: PropTypes.shape({ + tokens: PropTypes.array.isRequired, + getTokenProps: PropTypes.func.isRequired, + }), }; export default Shell;