code.tsx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { type FileContents, File, FileOptions, LineAnnotation } from "@pierre/precision-diffs"
  2. import { ComponentProps, createEffect, splitProps } from "solid-js"
  3. export type CodeProps<T = {}> = FileOptions<T> & {
  4. file: FileContents
  5. annotations?: LineAnnotation<T>[]
  6. class?: string
  7. classList?: ComponentProps<"div">["classList"]
  8. }
  9. export function Code<T>(props: CodeProps<T>) {
  10. let container!: HTMLDivElement
  11. const [local, others] = splitProps(props, ["file", "class", "classList", "annotations"])
  12. createEffect(() => {
  13. const instance = new File<T>({
  14. theme: "OpenCode",
  15. overflow: "wrap", // or 'scroll'
  16. themeType: "system", // 'system', 'light', or 'dark'
  17. disableFileHeader: true,
  18. disableLineNumbers: false, // optional
  19. // lang: 'typescript', // optional - auto-detected from filename if not provided
  20. ...others,
  21. })
  22. container.innerHTML = ""
  23. instance.render({
  24. file: local.file,
  25. lineAnnotations: local.annotations,
  26. containerWrapper: container,
  27. })
  28. })
  29. return (
  30. <div
  31. data-component="code"
  32. style={{
  33. "--pjs-font-family": "var(--font-family-mono)",
  34. "--pjs-font-size": "var(--font-size-small)",
  35. "--pjs-line-height": "24px",
  36. "--pjs-tab-size": 2,
  37. "--pjs-font-features": "var(--font-family-mono--font-feature-settings)",
  38. "--pjs-header-font-family": "var(--font-family-sans)",
  39. "--pjs-gap-block": 0,
  40. "--pjs-min-number-column-width": "4ch",
  41. }}
  42. classList={{
  43. ...(local.classList || {}),
  44. [local.class ?? ""]: !!local.class,
  45. }}
  46. ref={container}
  47. />
  48. )
  49. }