Skip to content

Commit

Permalink
Moves Debug component's styles to be inlined (#3963)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp authored Jul 18, 2022
1 parent aa06fd9 commit 5fde2fd
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/warm-news-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Makes the Debug component's styles be inlined
4 changes: 2 additions & 2 deletions packages/astro/components/Code.astro
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ const { code, lang = 'plaintext', theme = 'github-dark', wrap = false } = Astro.
/** Replace the shiki class name with a custom astro class name. */
function repairShikiTheme(html: string): string {
// Replace "shiki" class naming with "astro" and add "is:raw".
html = html.replace('<pre class="shiki"', '<pre is:raw class="astro-code"');
// Replace "shiki" class naming with "astro"
html = html.replace('<pre class="shiki"', '<pre class="astro-code"');
// Replace "shiki" css variable naming with "astro".
html = html.replace(/style="(background-)?color: var\(--shiki-/g, 'style="$1color: var(--astro-code-');
// Handle code wrapping
Expand Down
22 changes: 11 additions & 11 deletions packages/astro/components/Debug.astro
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,48 @@ const key = Object.keys(Astro.props)[0];
const value = Astro.props[key];
---

<div class="debug">
<div class="debug-header">
<h2 class="debug-title"><span class="debug-label">Debug</span> <span class="debug-name">"{key}"</span></h2>
<div class="astro-debug">
<div class="astro-debug-header">
<h2 class="astro-debug-title"><span class="astro-debug-label">Debug</span> <span class="astro-debug-name">"{key}"</span></h2>
</div>

<Code code={JSON.stringify(value, null, 2)} />
</div>

<style>
.debug {
<style is:inline>
.astro-debug {
font-size: 14px;
padding: 1rem 1.5rem;
background: white;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}

.debug-header,
pre {
.astro-debug-header,
pre.astro-code {
margin: -1rem -1.5rem 1rem;
padding: 0.25rem 0.75rem;
}

.debug-header {
.astro-debug-header {
background: #ff1639;
border-radius: 4px;
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
}

.debug-title {
.astro-debug-title {
font-size: 1em;
color: white;
margin: 0.5em 0;
}

.debug-label {
.astro-debug-label {
font-weight: bold;
text-transform: uppercase;
margin-right: 0.75em;
}

pre {
pre.astro-code {
border: 1px solid #eee;
padding: 1rem 0.75rem;
border-radius: 4px;
Expand Down
26 changes: 26 additions & 0 deletions packages/astro/test/code-component.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('Code component', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;

before(async () => {
fixture = await loadFixture({ root: './fixtures/code-component/' });
await fixture.build();
});

it('Debug component styles are not included in the page', async () => {
let html = await fixture.readFile('/index.html');
let $ = cheerio.load(html);
expect($('link[rel=stylesheet]')).to.have.a.lengthOf(0, 'No styles should be built');
expect($('style')).to.have.a.lengthOf(0);
});

it('is:raw attribute not serialized', async () => {
let html = await fixture.readFile('/index.html');
let $ = cheerio.load(html);
expect($('pre').attr('is:raw')).to.equal(undefined);
});
});
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/code-component/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/code-component",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
11 changes: 11 additions & 0 deletions packages/astro/test/fixtures/code-component/src/pages/index.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
import { Code } from 'astro/components';
---
<html>
<head>
<title>Testing</title>
</head>
<body>
<Code lang="js" code={`let foo = 'bar';`} />
</body>
</html>
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5fde2fd

Please sign in to comment.