Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

website: Change code sample box #1166

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 52 additions & 69 deletions apps/website/src/components/LiveExample.astro
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,34 @@ if (code.includes('Copyright (c) Bentley Systems')) {
}
---

<div class='demo-box'>
<ThemeProvider
className='demo-iui-wrapper'
theme='dark'
themeOptions={{ applyBackground: false }}
>
<slot />
</ThemeProvider>
<OpenInCodesandbox class='sandbox-link' code={code} />
<div class='demo-wrapper'>
<div class='demo-box'>
<ThemeProvider
className='demo-iui-wrapper'
theme='dark'
themeOptions={{ applyBackground: false }}
>
<slot />
</ThemeProvider>
<OpenInCodesandbox class='sandbox-link' code={code} />
</div>
<demo-code>
<details>
<summary>Code sample</summary>
<Code code={code} lang='tsx' theme={nightOwl} />
</details>
<button data-copied='false'><span class='visually-hidden'>Copy to clipboard</span></button>
</demo-code>
</div>
<demo-code>
{truncate && <button aria-expanded='false'>Toggle full code</button>}
<button data-copied='false'><span class='visually-hidden'>Copy to clipboard</span></button>
<Code code={code} lang='tsx' theme={nightOwl} />
</demo-code>

<script>
customElements.define(
'demo-code',
class extends HTMLElement {
connectedCallback() {
const expandButton = this.querySelector('button[aria-expanded]') as HTMLElement;
const copyButton = this.querySelector('button[data-copied]') as HTMLElement;
const code = this.querySelector('pre');

expandButton?.addEventListener('click', () => {
const shouldExpand = expandButton.getAttribute('aria-expanded') === 'false';
expandButton.setAttribute('aria-expanded', String(shouldExpand));
code.inert = !shouldExpand;
});

copyButton?.addEventListener('click', async () => {
try {
await navigator.clipboard.writeText(code.innerText);
Expand All @@ -79,16 +76,20 @@ if (code.includes('Copyright (c) Bentley Systems')) {
</script>

<style lang='scss'>
.demo-wrapper {
background: var(--color-sandbox-gradient);
border-radius: var(--border-radius-1);
}

.demo-box {
position: relative;
min-height: 300px;
max-height: 400px;
display: grid;
justify-items: center;
padding: 1rem;
background: var(--color-sandbox-gradient);
isolation: isolate;
border-radius: var(--border-radius-1);
border-radius: inherit;
}

.demo-iui-wrapper {
Expand All @@ -111,61 +112,43 @@ if (code.includes('Copyright (c) Bentley Systems')) {
isolation: isolate;
position: relative;
display: grid;
border-radius: var(--border-radius-1);
outline: 1px solid var(--color-line-2);
outline-offset: 1px;

> :global(pre) {
padding: 1rem;
font-size: var(--type--1);
border-radius: var(--border-radius-1);
}

> :global([aria-expanded='false'] ~ pre) {
max-height: 200px;
overflow: hidden !important;

&::before {
content: '';
position: absolute;
inset: 0;
z-index: 1;
background: linear-gradient(to bottom, transparent, var(--color-background-2));
> details {
border-radius: 0 0 var(--border-radius-1) var(--border-radius-1);
border-top: 1px solid var(--color-line-2);

> summary {
padding: 1rem;
cursor: pointer;
user-select: none;
color: var(--color-subtext);
border-radius: inherit;

&:hover {
background-color: var(--color-active);
color: var(--color-text);
}
}
}

> :global([aria-expanded='true'] ~ pre) {
padding-block-end: 2.5rem;
> :global(pre) {
padding: 1rem;
font-size: var(--type--1);
background-color: transparent !important;
}
}

button {
font: inherit;
cursor: pointer;
background: var(--color-background-1);
padding: 0.5rem 1.25rem;
background: transparent;
color: var(--color-text);
position: absolute;
border: 1px solid;
border-radius: var(--border-radius-1);
z-index: 2;
border: 1px solid transparent;
border-radius: 999px;
transition: color 0.2s, border-color 0.2s;
}

button[aria-expanded] {
bottom: 1rem;
left: 50%;
transform: translateX(-50%);

&:not(:hover, :focus-visible) {
color: var(--color-text);
border-color: var(--color-line-1);
}
}

button[data-copied] {
top: 0.5rem;
right: 0.5rem;
padding: 0.5rem;
border-color: var(--color-line-2);
top: 0.75rem;
right: 0.25rem;
padding: calc(0.5rem - 1px);
display: grid;
place-items: center;

Expand All @@ -185,7 +168,7 @@ if (code.includes('Copyright (c) Bentley Systems')) {
}

&:hover {
border-color: currentColor;
background-color: hsla(0deg, 0%, 0%, 0.2);
}
}

Expand Down