fractalsvelte mascot fractalsvelte fractalsvelte

Svelte 5 component · fractalsvelte-ui · Typography

Code

An inline monospace code token designed for highlighting keywords, identifiers, and keystrokes within prose. Offers soft background fills and defined borders to maintain visual distinction.

Install

Code is a Svelte 5 code component in @fractaldesign/fractalsvelte-ui. It works in SvelteKit and in any Svelte 5 app.

pnpm add @fractaldesign/fractalsvelte-ui

Import

import
import { Code } from '@fractaldesign/fractalsvelte-ui';

Usage

Example
<Code variant={'subtle'} size={'md'}>Click me</Code>

Props

Also accepts every attribute of HTMLAttributes, spread onto the root element.

NameTypeDefault
variant CodeVariant 'subtle' | 'outline' | 'surface' | 'primary' 'subtle'
size CodeSize 'sm' | 'md' | 'lg' 'md'
class string ''

Snippets

NameTypeDefault
children Snippet —

Source

Show the Code.svelte source
Code.svelte
<script lang="ts">
	import type { Snippet } from 'svelte';
	import type { HTMLAttributes } from 'svelte/elements';

	export type CodeVariant = 'subtle' | 'outline' | 'surface' | 'primary';
	export type CodeSize = 'sm' | 'md' | 'lg';

	interface Props extends HTMLAttributes<HTMLElement> {
		variant?: CodeVariant;
		size?: CodeSize;
		class?: string;
		children?: Snippet;
	}

	let {
		variant = 'subtle',
		size = 'md',
		class: className = '',
		children,
		...restProps
	}: Props = $props();
</script>

<code
	class="f-code {className}"
	data-variant={variant}
	data-size={size}
	{...restProps}
>
	{#if children}
		{@render children()}
	{/if}
</code>

<style>
	@layer fractalsvelte-ui.components {
		.f-code {
			font-family: var(--f-font-mono);
			font-weight: 450;
			line-height: normal;
			border-radius: var(--f-radius-xs);
			box-sizing: border-box;
			white-space: break-spaces;
			word-break: break-word;
		}

		/* Sizes */
		.f-code[data-size='sm'] {
			font-size: 0.75em;
			padding: 1px 4px;
		}

		.f-code[data-size='md'] {
			font-size: 0.85em;
			padding: 2px 6px;
		}

		.f-code[data-size='lg'] {
			font-size: 0.95em;
			padding: 3px 8px;
		}

		/* Variants */
		.f-code[data-variant='subtle'] {
			background: var(--f-sunken);
			color: var(--f-text);
			border: 1px solid var(--f-border-subtle);
		}

		.f-code[data-variant='outline'] {
			background: transparent;
			color: var(--f-text);
			border: 1px solid var(--f-border-strong);
		}

		.f-code[data-variant='surface'] {
			background: var(--f-surface);
			color: var(--f-text);
			border: 1px solid var(--f-border);
			box-shadow: var(--f-shadow-xs);
		}

		.f-code[data-variant='primary'] {
			background: var(--f-primary-subtle);
			color: var(--f-primary);
			border: 1px solid rgba(16, 185, 129, 0.2);
		}
	}
</style>