|
| 1 | +import { faExclamationTriangle } from "fa-solid"; |
| 2 | +import { getTextWidth } from "./get-text-size.ts"; |
| 3 | +import { renderToString } from "preact-render-to-string"; |
| 4 | +import { loadEmbeddableFontSync } from "./load-font.ts"; |
| 5 | +import { Icon } from "./icon.tsx"; |
| 6 | +import { css } from "./css-fake-function.ts"; |
| 7 | + |
| 8 | +export function Error( |
| 9 | + { error }: { |
| 10 | + error: string; |
| 11 | + }, |
| 12 | +) { |
| 13 | + const title = "An error occurred while rendering the badge."; |
| 14 | + |
| 15 | + const widgetWidth = Math.max(getTextWidth(title), getTextWidth(error, 14)) + 10 + 40; // 10px padding on each side |
| 16 | + const lineHeight = 60; // Height of each line in the SVG |
| 17 | + |
| 18 | + const robotFont = loadEmbeddableFontSync( |
| 19 | + "./rendering/roboto/Roboto-VariableFont_wdth,wght.woff2", |
| 20 | + ); |
| 21 | + |
| 22 | + return ( |
| 23 | + <svg |
| 24 | + xmlns="http://www.w3.org/2000/svg" |
| 25 | + xmlnsXlink="http://www.w3.org/1999/xlink" |
| 26 | + width={widgetWidth} |
| 27 | + height={lineHeight} |
| 28 | + > |
| 29 | + <defs> |
| 30 | + <style> |
| 31 | + {css` |
| 32 | + @font-face { |
| 33 | + font-family: "Roboto"; |
| 34 | + src: url("${robotFont}") format("woff2"); |
| 35 | + font-weight: normal; |
| 36 | + font-style: normal; |
| 37 | + } |
| 38 | +
|
| 39 | + text { |
| 40 | + font-family: "Roboto", "Arial", sans-serif; |
| 41 | + } |
| 42 | +
|
| 43 | + :root { |
| 44 | + --badge-bg-color: #f8f9fa; |
| 45 | + --badge-border-color: #e9ecef; |
| 46 | + --badge-text-color: #000; |
| 47 | + --badge-text-color-light: #666; |
| 48 | + --badge-checkmark-color: #34a853; |
| 49 | + --badge-xmark-color: #ea4335; |
| 50 | + --badge-error-color: #ff6961; |
| 51 | + } |
| 52 | +
|
| 53 | + @media (prefers-color-scheme: dark) { |
| 54 | + :root { |
| 55 | + --badge-bg-color: #343a40; |
| 56 | + --badge-border-color: #495057; |
| 57 | + --badge-text-color: #f8f9fa; |
| 58 | + --badge-text-color-light: #adb5bd; |
| 59 | + --badge-checkmark-color: #28a745; |
| 60 | + --badge-xmark-color: #dc3545; |
| 61 | + } |
| 62 | + } |
| 63 | +
|
| 64 | + a { |
| 65 | + text-decoration: none; |
| 66 | + } |
| 67 | +
|
| 68 | + a:hover { |
| 69 | + fill: #5a7d9f; |
| 70 | + text-decoration: underline; |
| 71 | + } |
| 72 | + `} |
| 73 | + </style> |
| 74 | + </defs> |
| 75 | + <rect |
| 76 | + width={widgetWidth} |
| 77 | + height={lineHeight} |
| 78 | + rx="8" |
| 79 | + ry="8" |
| 80 | + fill="var(--badge-bg-color)" |
| 81 | + stroke="var(--badge-border-color)" |
| 82 | + strokeWidth="1" |
| 83 | + /> |
| 84 | + <Icon icon={faExclamationTriangle} size={20} fill="var(--badge-error-color)" x={10} y={10} /> |
| 85 | + <text |
| 86 | + x={40} |
| 87 | + y="25" |
| 88 | + fontSize="16" |
| 89 | + fill="var(--badge-text-color-light)" |
| 90 | + > |
| 91 | + <a href="https://github.com/K0IN/can-i-use-embed"> |
| 92 | + {title} |
| 93 | + <title>Click to open the documentation.</title> |
| 94 | + </a> |
| 95 | + </text> |
| 96 | + |
| 97 | + <text |
| 98 | + x={40} |
| 99 | + y="50" |
| 100 | + fontSize="14" |
| 101 | + fill="var(--badge-text-color-light)" |
| 102 | + > |
| 103 | + {error} |
| 104 | + </text> |
| 105 | + </svg> |
| 106 | + ); |
| 107 | +} |
| 108 | + |
| 109 | +export function RenderError(args: Parameters<typeof Error>[0]): string { |
| 110 | + return renderToString(<Error {...args} />); |
| 111 | +} |
0 commit comments