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

Enhanced exports #50

Merged
merged 4 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
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
12 changes: 3 additions & 9 deletions src/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,6 @@ tbody tr:last-child td {
padding-right: 0;
}

pre {
border-radius: var(--radius);
padding: 1rem;
background: var(--bg-hard);
font: inherit;
font-family: var(--font-mono);
overflow-x: auto;
}

.h2 {
margin-bottom: var(--vgap);
text-transform: lowercase;
Expand Down Expand Up @@ -361,6 +352,9 @@ input[type='range'] {
.data-options {
position: sticky;
top: 1rem;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(28ch, 1fr));
gap: 1rem;
min-width: 20ch;
}

Expand Down
3 changes: 0 additions & 3 deletions src/lib/components/combinations/Combinations.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@

<style>
.data-options {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(28ch, 1fr));
gap: 1rem;
animation: slide-up 250ms ease-out;
contain: layout;
}
Expand Down
19 changes: 19 additions & 0 deletions src/lib/components/export/CodeArea.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<script lang="ts">
export let code: string;
</script>

<textarea class="elevated" rows="10" readonly>{code}</textarea>

<style>
textarea {
align-self: stretch;
border: 0;
border-radius: var(--radius);
padding: 1rem;
background: var(--bg-hard);
font: inherit;
font-family: var(--font-mono);
resize: none;
white-space: pre;
}
</style>
5 changes: 4 additions & 1 deletion src/lib/components/export/DesignTokensExport.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import {permalink} from '$lib/routing';
import type {Entry} from '$lib/stores/combinations';
import {rootNode} from '$lib/stores/palette';
import CodeArea from './CodeArea.svelte';

export let entries: Entry[];

Expand All @@ -24,6 +25,8 @@
...tokens
}
};

$: code = JSON.stringify(json, null, 2);
</script>

<pre class="elevated">{JSON.stringify(json, null, 2)}</pre>
<CodeArea {code} />
34 changes: 24 additions & 10 deletions src/lib/components/export/Export.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
exportCasing,
exportColorFormat,
exportFormat,
exportPrecision
exportPrecision,
exportPrefix
} from '$lib/stores/export';
import DesignTokensExport from './DesignTokensExport.svelte';

Expand Down Expand Up @@ -181,18 +182,30 @@
</div>
</div>
</fieldset>

<div>
<label for="prefix">Variables prefix</label>
<input
id="prefix"
name="prefix"
type="text"
bind:value={$exportPrefix}
/>
</div>
{/if}

{#if $exportFormat !== 'tokens' && $exportColorFormat !== 'hex' && $exportColorFormat !== 'rgb'}
<label for="precision">Precision</label>
<input
id="precision"
name="precision"
type="number"
min={0}
max={5}
bind:value={$exportPrecision}
/>
<div>
<label for="precision">Precision</label>
<input
id="precision"
name="precision"
type="number"
min={0}
max={5}
bind:value={$exportPrecision}
/>
</div>
{/if}
</div>
</div>
Expand All @@ -206,6 +219,7 @@
{entries}
format={$exportFormat}
casing={$exportCasing}
prefix={$exportPrefix}
/>
{/if}
</div>
Expand Down
13 changes: 9 additions & 4 deletions src/lib/components/export/VariablesExport.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@
import {rootNode} from '$lib/stores/palette';
import {cssRule, sassRule} from '$lib/utils/css';
import {camelCase, dashCase} from '$lib/utils/strings';
import CodeArea from './CodeArea.svelte';

export let entries: ExportEntry[];
export let format: ExportFormat;
export let casing: ExportCasing;
export let prefix: string;

$: formatRule = format === 'css' ? cssRule : sassRule;
$: formatCase = casing === 'dash' ? dashCase : camelCase;
$: variables = entries
.map(({name, cssColor}) =>
formatRule(formatCase(name), cssColor)
formatRule(
formatCase(prefix ? `${prefix} ${name}` : name),
cssColor
)
)
.join('\n');

$: code = `/* @see ${permalink($rootNode)} */\n${variables}`;
</script>

<pre class="elevated">/* @see {permalink($rootNode)} */
{variables}
</pre>
<CodeArea {code} />
1 change: 1 addition & 0 deletions src/lib/stores/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export const exportFormat = writable<ExportFormat>('table');
export const exportColorFormat = writable<ColorFormat>('hex');
export const exportCasing = writable<ExportCasing>('dash');
export const exportPrecision = writable(3);
export const exportPrefix = writable('');
7 changes: 7 additions & 0 deletions src/lib/utils/strings.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
// Sass is more liberal than CSS in what chars are legal in
// a variable name, but we're taking the stricter approach.
const stripForbiddenChars = (text: string) =>
text.replaceAll(/[^a-zA-Z0-9_-]/g, '');

export const sentenceCase = (sentence: string) =>
sentence.charAt(0).toUpperCase() +
sentence.slice(1).toLowerCase();

export const dashCase = (sentence: string) =>
sentence
.split(' ')
.map(stripForbiddenChars)
.map((word) => word.toLowerCase())
.join('-');

export const camelCase = (sentence: string) =>
sentence
.split(' ')
.map(stripForbiddenChars)
.map((word, index) =>
index === 0 ? word.toLowerCase() : sentenceCase(word)
)
Expand Down
Loading