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} ` : ''} +