From a4c9b9c8cf8dffca413c00230acac001b331a351 Mon Sep 17 00:00:00 2001 From: Cory LaViska Date: Thu, 4 Nov 2021 07:27:18 -0400 Subject: [PATCH] add react support into lib --- .gitignore | 1 + docs/_sidebar.md | 5 + docs/assets/plugins/code-block/code-block.css | 23 +- docs/assets/plugins/code-block/code-block.js | 244 ++++++++++++++---- docs/assets/plugins/metadata/metadata.js | 70 ++--- docs/assets/styles/docs.css | 1 - docs/frameworks/angular.md | 22 ++ docs/frameworks/react.md | 88 +++++++ docs/frameworks/vue.md | 71 +++++ docs/getting-started/installation.md | 8 +- docs/getting-started/overview.md | 5 +- docs/getting-started/usage.md | 136 +--------- docs/index.html | 7 +- docs/resources/changelog.md | 3 +- docs/tutorials/integrating-with-nextjs.md | 2 +- package-lock.json | 115 +++++++++ package.json | 3 + scripts/build.js | 16 +- scripts/make-react.js | 66 +++++ scripts/shared.js | 3 +- 20 files changed, 652 insertions(+), 237 deletions(-) create mode 100644 docs/frameworks/angular.md create mode 100644 docs/frameworks/react.md create mode 100644 docs/frameworks/vue.md create mode 100644 scripts/make-react.js diff --git a/.gitignore b/.gitignore index 09e622f0..6db0b64a 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ docs/search.json dist examples node_modules +src/react diff --git a/docs/_sidebar.md b/docs/_sidebar.md index 3879a8d1..e0f2f129 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -5,6 +5,11 @@ - [Themes](/getting-started/themes) - [Customizing](/getting-started/customizing) +- Frameworks + - [React](/frameworks/react) + - [Vue](/frameworks/vue) + - [Angular](/frameworks/angular) + - Resources - [Community](/resources/community) - [Contributing](/resources/contributing) diff --git a/docs/assets/plugins/code-block/code-block.css b/docs/assets/plugins/code-block/code-block.css index 25a7f2a2..c4d9060a 100644 --- a/docs/assets/plugins/code-block/code-block.css +++ b/docs/assets/plugins/code-block/code-block.css @@ -61,11 +61,17 @@ border: solid 1px rgb(var(--sl-color-neutral-200)); border-bottom: none; border-radius: 0 !important; - margin: 0; display: none; } -.code-block--expanded .code-block__source { +.code-block__source pre { + margin: 0; +} + +.code-block--expanded.code-block--show-html .code-block__source--html, +.code-block--expanded.code-block--show-react .code-block__source--react, +/* When a code block is expanded but doesn't have a React example, force the HTML example to show */ +.code-block--expanded:not(.code-block--has-react) .code-block__source--html { display: block; } @@ -97,6 +103,19 @@ border-right: solid 1px rgb(var(--sl-color-neutral-200)); } +.code-block__button--html, +.code-block__button--react { + width: 70px; + display: flex; + place-items: center; + justify-content: center; +} + +.code-block__button--selected { + font-weight: 700; + color: rgb(var(--sl-color-primary-600)); +} + .code-block__button--codepen { display: flex; place-items: center; diff --git a/docs/assets/plugins/code-block/code-block.js b/docs/assets/plugins/code-block/code-block.js index 2f757742..1f2f889d 100644 --- a/docs/assets/plugins/code-block/code-block.js +++ b/docs/assets/plugins/code-block/code-block.js @@ -1,10 +1,30 @@ (() => { + const version = sessionStorage.getItem('sl-version'); + let flavor = localStorage.getItem('flavor') || 'html'; // "html" or "react" let count = 1; if (!window.$docsify) { throw new Error('Docsify must be loaded before installing this plugin.'); } + function convertModuleLinks(html) { + return html.replace(/@shoelace-style\/shoelace/g, `https://cdn.skypack.dev/@shoelace-style/shoelace@${version}`); + } + + function getAdjacentExample(name, pre) { + let currentPre = pre.nextElementSibling; + + while (currentPre?.tagName.toLowerCase() === 'pre') { + if (currentPre?.getAttribute('data-lang').split(' ').includes(name)) { + return currentPre; + } + + currentPre = currentPre.nextElementSibling; + } + + return null; + } + function runScript(script) { const newScript = document.createElement('script'); @@ -28,36 +48,66 @@ hook.afterEach(function (html, next) { const domParser = new DOMParser(); const doc = domParser.parseFromString(html, 'text/html'); - const codePenButton = ` - - `; + HTML + + `; + + const reactButton = ` + + `; + + const codePenButton = ` + + `; [...doc.querySelectorAll('code[class^="lang-"]')].map(code => { if (code.classList.contains('preview')) { const pre = code.closest('pre'); const preId = `code-block-preview-${count}`; const toggleId = `code-block-toggle-${count}`; + const reactPre = getAdjacentExample('react', pre); + const hasReact = reactPre !== null; + const examples = [ + { + name: 'HTML', + codeBlock: pre + } + ]; pre.id = preId; - pre.classList.add('code-block__source'); pre.setAttribute('data-lang', pre.getAttribute('data-lang').replace(/ preview$/, '')); pre.setAttribute('aria-labelledby', toggleId); const codeBlock = ` -
+
${code.textContent}
@@ -65,9 +115,23 @@
- ${pre.outerHTML} +
+ ${pre.outerHTML} +
+ + ${ + hasReact + ? ` +
+ ${reactPre.outerHTML} +
+ ` + : '' + }
+ ${hasReact ? ` ${htmlButton} ${reactButton} ` : ''} +