content-bash.tsx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import style from "./content-bash.module.css"
  2. import { createResource, createSignal } from "solid-js"
  3. import { createOverflow } from "./common"
  4. import { codeToHtml } from "shiki"
  5. interface Props {
  6. command: string
  7. output: string
  8. description?: string
  9. expand?: boolean
  10. }
  11. export function ContentBash(props: Props) {
  12. const [commandHtml] = createResource(
  13. () => props.command,
  14. async (command) => {
  15. return codeToHtml(command || "", {
  16. lang: "bash",
  17. themes: {
  18. light: "github-light",
  19. dark: "github-dark",
  20. },
  21. })
  22. },
  23. )
  24. const [outputHtml] = createResource(
  25. () => props.output,
  26. async (output) => {
  27. return codeToHtml(output || "", {
  28. lang: "console",
  29. themes: {
  30. light: "github-light",
  31. dark: "github-dark",
  32. },
  33. })
  34. },
  35. )
  36. const [expanded, setExpanded] = createSignal(false)
  37. const overflow = createOverflow()
  38. return (
  39. <div class={style.root} data-expanded={expanded() || props.expand === true ? true : undefined}>
  40. <div data-slot="body">
  41. <div data-slot="header">
  42. <span>{props.description}</span>
  43. </div>
  44. <div data-slot="content">
  45. <div innerHTML={commandHtml()} />
  46. <div data-slot="output" ref={overflow.ref} innerHTML={outputHtml()} />
  47. </div>
  48. </div>
  49. {!props.expand && overflow.status && (
  50. <button
  51. type="button"
  52. data-component="text-button"
  53. data-slot="expand-button"
  54. onClick={() => setExpanded((e) => !e)}
  55. >
  56. {expanded() ? "Show less" : "Show more"}
  57. </button>
  58. )}
  59. </div>
  60. )
  61. }