Skip to content

Commit

Permalink
feat: customize font size
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Feb 9, 2025
1 parent ceb9368 commit f17977d
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 36 deletions.
1 change: 1 addition & 0 deletions site/app/live-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export function LiveEditor({
className="editor"
title={title}
controls={controls}
fontSize={'14px'}
lineNumbers={lineNumbers}
onChange={(text) => setCode(text)}
/>
Expand Down
7 changes: 5 additions & 2 deletions site/app/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
--sh-property: #7be5f2;
--codice-control-color: #8d8989;
--codice-caret-color: #d5efea;
--codice-font-size: 14px;
}

* {
Expand Down Expand Up @@ -79,6 +80,9 @@ input[type=radio] {
.section {
margin: 4rem 0;
}
.section h2 {
margin: 0;
}

/* Code Example */
.code-example [data-codice-code] {
Expand All @@ -92,15 +96,14 @@ input[type=radio] {
}

.code-example pre code {
font-size: 1rem;
line-height: 1.5;
}

.code-example__item {
padding: 1rem;
background-color: #3d3d3d;
border-radius: 0.5rem;
margin: 3rem 0;
margin: 2rem 0;
border: 1px solid rgba(163, 169, 165, 0.2);
}
.code-example__item__title {
Expand Down
10 changes: 6 additions & 4 deletions src/code/code.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { tokenize, generate } from 'sugar-high'
import { baseCss, headerCss, lineNumbersCss } from './css'
import { baseCss, headerCss, lineNumbersCss, fontCss } from './css'
import * as presets from 'sugar-high/presets'
import { useMemo } from 'react'

Expand Down Expand Up @@ -126,22 +126,24 @@ export function Code({
children: code,
title,
controls,
fontSize,
highlightLines,
preformatted = true,
lineNumbers = false,
highlightLines,
asMarkup = false,
...props
}: {
children: string
/** Whether to use a preformatted block <pre><code> */
preformatted?: boolean
fontSize?: number
highlightLines?: ([number, number] | number)[]
title?: string
controls?: boolean
lineNumbers?: boolean
highlightLines?: ([number, number] | number)[]
asMarkup?: boolean
} & React.HTMLAttributes<HTMLDivElement>) {
const css = baseCss + (lineNumbers ? lineNumbersCss : '')
const css = baseCss + (lineNumbers ? lineNumbersCss : '') + fontCss(fontSize)
const lineElements = useMemo(() =>
asMarkup
? code
Expand Down
37 changes: 19 additions & 18 deletions src/code/css.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const C = `[data-codice-code]`
const H = `[data-codice-header]`
const C = `:scope [data-codice-code]`
const H = `:scope [data-codice-header]`

export const baseCss = `\
${C} {
Expand Down Expand Up @@ -63,21 +63,22 @@ ${H} [data-codice-control] {
`

export const lineNumbersCss = `\
@scope {
code {
counter-reset: codice-code-line-number;
padding-left
}
[data-codice-code-line-number] {
counter-increment: codice-code-line-number 1;
content: counter(codice-code-line-number);
display: inline-block;
min-width: 24px;
margin-left: -40px;
margin-right: 16px;
text-align: right;
user-select: none;
color: var(--codice-code-line-number-color);
}
:scope code {
counter-reset: codice-code-line-number;
}
:scope [data-codice-code-line-number] {
counter-increment: codice-code-line-number 1;
content: counter(codice-code-line-number);
display: inline-block;
min-width: 24px;
margin-left: -40px;
margin-right: 16px;
text-align: right;
user-select: none;
color: var(--codice-code-line-number-color);
}
`

export const fontCss = (fontSize: number | undefined) => `\
:scope { font-size: ${fontSize || 'inherit'}; }
`
17 changes: 9 additions & 8 deletions src/editor/css.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
const R = '[data-codice="editor"]'
const R = ':scope'
export const css = `\
${R} {
--codice-text-color: transparent;
--codice-background-color: transparent;
--codice-caret-color: inherit;
}
${R} {
position: relative;
overflow-y: scroll;
display: flex;
flex-direction: column;
justify-content: stretch;
scrollbar-width: none;
--codice-text-color: transparent;
--codice-background-color: transparent;
--codice-caret-color: inherit;
scrollbar-width: none;
}
${R} code,
${R} textarea {
Expand All @@ -19,8 +20,8 @@ ${R} textarea {
overflow-wrap: break-word;
scrollbar-width: none;
padding: 24px 16px;
font-size: 16px;
line-height: 20px;
font-size: var(--codice-font-size);
caret-color: var(--codice-caret-color);
border: none;
outline: none;
Expand Down Expand Up @@ -51,7 +52,7 @@ ${R} textarea {
height: 100%;
overflow: hidden;
}
${R}[data-codice-line-numbers="true"] textarea {
${R} [data-codice-line-numbers="true"] textarea {
padding-left: 55px;
}
`
Expand Down
3 changes: 3 additions & 0 deletions src/editor/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ const Editor = forwardRef(function EditorComponent(
value = '',
controls,
lineNumbers,
fontSize,
onChange = () => {},
}: {
title?: string
value?: string
controls?: boolean
lineNumbers?: boolean
fontSize?: number
onChange?: (code: string) => void
} & React.HTMLAttributes<HTMLDivElement>,
ref: React.Ref<HTMLDivElement>
Expand Down Expand Up @@ -65,6 +67,7 @@ const Editor = forwardRef(function EditorComponent(
title={null}
controls={false}
lineNumbers={lineNumbers}
fontSize={fontSize}
>
{code}
</Code>
Expand Down
19 changes: 15 additions & 4 deletions src/editor/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { Editor as EditorClient } from './editor'
import { css } from './css'

export type EditorProps = React.HTMLAttributes<HTMLDivElement> & Parameters<typeof EditorClient>[0]
export type EditorProps = React.HTMLAttributes<HTMLDivElement>
& Parameters<typeof EditorClient>[0]

export function Editor(props: EditorProps) {
const { title, value, onChange, controls = true, lineNumbers = true, ...restProps } = props
const editorProps = { title, value, onChange, controls, lineNumbers }
const {
title,
value,
onChange,
controls = true,
lineNumbers = true,
fontSize,
...restProps
} = props
const editorProps = { title, value, onChange, controls, lineNumbers, fontSize }

return (
<div
Expand All @@ -17,7 +26,9 @@ export function Editor(props: EditorProps) {
data-codice-controls={!!controls}
data-codice-line-numbers={!!lineNumbers}
>
<style data-codice-style>{css}</style>
<style data-codice-style>
{css}
</style>
<EditorClient {...editorProps} />
</div>
)
Expand Down

0 comments on commit f17977d

Please sign in to comment.