Inline Text
Code Element
Style inline code and preformatted text blocks with configurable font family, font size, and spacing using the useCodeElement and usePreElement composables.
Part of the Global Preset:
useCodeElement and usePreElement are included in the Global Preset (useGlobalPreset) and you can configure them through the preset's code and pre options. For most projects, applying them via the preset is the recommended approach.Overview
The code composables style inline code snippets and preformatted text blocks. The useCodeElement composable targets code and samp elements, while usePreElement configures pre blocks with scroll overflow and nested code resets.
useCodeElement
function useCodeElement(
s: Styleframe,
options?: WithThemes<CodeElementConfig>
): CodeElementResult
styleframe.config.ts
import { styleframe } from 'styleframe';
import { useCodeElement } from '@styleframe/theme';
const s = styleframe();
const { codeFontFamily, codeFontSize } = useCodeElement(s);
export default s;
styleframe/index.css
:root {
--code--font-family: var(--font-family--mono);
--code--font-size: 0.875em;
}
code, samp {
font-family: var(--code--font-family);
font-size: var(--code--font-size);
}
Default Values
| Option | Default | Variable | Export |
|---|---|---|---|
fontFamily | "@font-family.mono" | --code--font-family | codeFontFamily |
fontSize | "0.875em" | --code--font-size | codeFontSize |
usePreElement
function usePreElement(
s: Styleframe,
options?: WithThemes<PreElementConfig>
): PreElementResult
styleframe.config.ts
import { styleframe } from 'styleframe';
import { usePreElement } from '@styleframe/theme';
const s = styleframe();
const { preFontFamily, preFontSize, preMarginBottom } = usePreElement(s);
export default s;
styleframe/index.css
:root {
--pre--font-family: var(--font-family--mono);
--pre--font-size: 0.875em;
--pre--margin-bottom: var(--spacing);
}
pre {
font-family: var(--pre--font-family);
font-size: var(--pre--font-size);
display: block;
overflow-x: auto;
margin-top: 0;
margin-bottom: var(--pre--margin-bottom);
}
pre > code {
background: transparent;
color: inherit;
font-size: inherit;
}
Default Values
| Option | Default | Variable | Export |
|---|---|---|---|
fontFamily | "@font-family.mono" | --pre--font-family | preFontFamily |
fontSize | "0.875em" | --pre--font-size | preFontSize |
marginBottom | "@spacing" | --pre--margin-bottom | preMarginBottom |
The composable also applies fixed styles: display: block, overflow-x: auto, and margin-top: 0. Nested code elements inside pre have their background, color, and font size reset to inherit from the parent.
Custom Values
styleframe.config.ts
import { styleframe } from 'styleframe';
import { useCodeElement, usePreElement } from '@styleframe/theme';
const s = styleframe();
const { codeFontFamily } = useCodeElement(s, {
fontFamily: '"Fira Code", monospace',
fontSize: '0.8em',
});
const { preMarginBottom } = usePreElement(s, {
fontFamily: '"Fira Code", monospace',
marginBottom: '@spacing.lg',
});
export default s;
styleframe.config.ts
import { styleframe } from 'styleframe';
import { useGlobalPreset } from '@styleframe/theme';
const s = styleframe();
const { code, pre } = useGlobalPreset(s, {
code: {
fontFamily: '"Fira Code", monospace',
fontSize: '0.8em',
},
pre: {
fontFamily: '"Fira Code", monospace',
marginBottom: '@spacing.lg',
},
});
export default s;
styleframe/index.css
:root {
--code--font-family: "Fira Code", monospace;
--code--font-size: 0.8em;
--pre--font-family: "Fira Code", monospace;
--pre--font-size: 0.875em;
--pre--margin-bottom: var(--spacing--lg);
}
code, samp {
font-family: var(--code--font-family);
font-size: var(--code--font-size);
}
pre {
font-family: var(--pre--font-family);
font-size: var(--pre--font-size);
display: block;
overflow-x: auto;
margin-top: 0;
margin-bottom: var(--pre--margin-bottom);
}
pre > code {
background: transparent;
color: inherit;
font-size: inherit;
}
Best Practices
- Use a monospace font: Both composables default to
@font-family.monofor consistent code rendering. - Use
emunits for font size: The default0.875emscales relative to the parent, keeping inline code proportional to surrounding text. - Keep both in sync: When customizing the font family, apply the same value to both
useCodeElementandusePreElementfor visual consistency. - Rely on the nested code reset: The
pre > codereset ensures inline code styles do not leak into code blocks.