Class: Sandals::HTMLRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/sandals/html_renderer.rb

Constant Summary collapse

TURBO_URL =
"/__sandals/assets/turbo.js"
TURBO_PATH =
File.expand_path("assets/turbo.es2017-umd.js", __dir__)
DEFAULT_STYLES =
<<~CSS
  :root {
    color-scheme: light;
    --color-canvas: #f7f6f2;
    --color-surface: #ffffff;
    --color-surface-subtle: #efede7;
    --color-text: #242320;
    --color-muted: #6b6963;
    --color-border: #d8d5cc;
    --color-border-strong: #aaa69b;
    --color-accent: #6558d3;
    --color-accent-soft: #eeecff;
    --color-danger: #a9362a;
    --color-danger-soft: #fff0ed;
    --color-success: #187451;
    --color-success-soft: #eaf8f1;
    --radius-control: 0.5rem;
    --radius-panel: 0.75rem;
    --shadow-focus: 0 0 0 3px rgb(101 88 211 / 20%);
    --space-1: 0.375rem;
    --space-2: 0.75rem;
    --space-3: 1rem;
    --space-4: 1.5rem;
    --space-5: 2rem;
    --space-6: 3rem;
    font-family: ui-sans-serif, system-ui, -apple-system, sans-serif;
    background: var(--color-canvas);
    color: var(--color-text);
  }

  *,
  *::before,
  *::after {
    box-sizing: border-box;
  }

  body {
    margin: 0;
    min-width: 0;
    background: var(--color-canvas);
    font-size: 1rem;
    line-height: 1.5;
  }

  main {
    width: min(100%, 48rem);
    margin: 0 auto;
    padding: clamp(var(--space-4), 6vw, var(--space-6));
  }

  h1 {
    margin: 0;
    max-width: 18ch;
    font-size: clamp(2.25rem, 7vw, 3.75rem);
    line-height: 0.98;
    letter-spacing: -0.045em;
    text-wrap: balance;
    overflow-wrap: anywhere;
  }

  h2 {
    margin: 0;
    font-size: clamp(1.35rem, 4vw, 1.75rem);
    line-height: 1.15;
    letter-spacing: -0.02em;
    text-wrap: balance;
  }

  p {
    margin: 0;
    max-width: 68ch;
    line-height: 1.65;
    overflow-wrap: anywhere;
  }

  .error {
    padding: var(--space-2) var(--space-3);
    border: 1px solid #efb2aa;
    border-radius: var(--radius-control);
    background: var(--color-danger-soft);
    color: var(--color-danger);
  }

  .field-error {
    color: var(--color-danger);
  }

  .notice {
    padding: var(--space-2) var(--space-3);
    border: 1px solid #a9dec6;
    border-radius: var(--radius-control);
    background: var(--color-success-soft);
    color: var(--color-success);
  }

  .runtime-errors {
    width: min(100%, 48rem);
    margin: 0 auto;
    padding: var(--space-3) var(--space-4) 0;
  }

  .action-error {
    display: grid;
    gap: var(--space-1);
    padding: var(--space-3);
    border: 1px solid #efb2aa;
    border-radius: var(--radius-control);
    background: var(--color-danger-soft);
    color: var(--color-danger);
  }

  .action-error code {
    overflow-wrap: anywhere;
  }

  .field-error {
    font-size: 0.875rem;
    font-weight: 450;
  }

  img {
    display: block;
    max-width: 100%;
    height: auto;
    border-radius: var(--radius-panel);
  }

  a {
    color: var(--color-accent);
    font-weight: 550;
    text-underline-offset: 0.15em;
    text-decoration-thickness: 0.08em;
  }

  a:hover {
    text-decoration-thickness: 0.14em;
  }

  a:focus-visible,
  button:focus-visible,
  input:focus-visible,
  textarea:focus-visible,
  select:focus-visible {
    outline: 2px solid var(--color-accent);
    outline-offset: 2px;
  }

  .text-field,
  .number-field,
  .text-area,
  .file-field,
  .select {
    display: grid;
    min-width: 0;
    gap: var(--space-1);
    font-weight: 600;
  }

  .text-field input,
  .number-field input,
  .text-area textarea,
  .file-field input,
  .select select {
    width: 100%;
    min-height: 2.75rem;
    padding: 0.625rem var(--space-2);
    border: 1px solid var(--color-border-strong);
    border-radius: var(--radius-control);
    background: var(--color-surface);
    color: inherit;
    font: inherit;
    font-weight: 400;
    transition: border-color 120ms ease, box-shadow 120ms ease;
  }

  .text-field input:hover,
  .number-field input:hover,
  .text-area textarea:hover,
  .file-field input:hover,
  .select select:hover {
    border-color: var(--color-text);
  }

  .text-field input:focus,
  .number-field input:focus,
  .text-area textarea:focus,
  .file-field input:focus,
  .select select:focus {
    border-color: var(--color-accent);
    box-shadow: var(--shadow-focus);
  }

  input[aria-invalid="true"],
  textarea[aria-invalid="true"],
  select[aria-invalid="true"] {
    border-color: var(--color-danger);
    background: var(--color-danger-soft);
  }

  .text-area textarea {
    min-height: 7rem;
    resize: vertical;
  }

  .checkbox {
    display: flex;
    align-items: center;
    min-width: 0;
    gap: var(--space-2);
    font-weight: 550;
  }

  .checkbox input {
    width: 1.125rem;
    height: 1.125rem;
    margin: 0;
    accent-color: var(--color-accent);
  }

  .radio {
    display: grid;
    min-width: 0;
    gap: var(--space-2);
    margin: 0;
    padding: 0;
    border: 0;
  }

  .radio legend {
    margin-bottom: var(--space-1);
    font-weight: 600;
  }

  .radio label {
    display: flex;
    align-items: center;
    gap: var(--space-2);
  }

  .radio input {
    margin: 0;
    accent-color: var(--color-accent);
  }

  .table-scroll {
    max-width: 100%;
    overflow-x: auto;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-panel);
    background: var(--color-surface);
    overscroll-behavior-inline: contain;
  }

  table {
    width: 100%;
    min-width: 36rem;
    border-collapse: collapse;
    background: var(--color-surface);
  }

  th,
  td {
    padding: var(--space-2) var(--space-3);
    border-bottom: 1px solid var(--color-border);
    text-align: left;
    vertical-align: top;
    overflow-wrap: anywhere;
  }

  th {
    background: var(--color-surface-subtle);
    font-weight: 600;
  }

  tbody tr:last-child td {
    border-bottom: 0;
  }

  button {
    min-height: 2.75rem;
    padding: 0.625rem var(--space-3);
    border: 1px solid var(--color-text);
    border-radius: var(--radius-control);
    background: var(--color-text);
    color: white;
    font: inherit;
    font-weight: 650;
    cursor: pointer;
    transition: transform 100ms ease, opacity 100ms ease, background 100ms ease;
  }

  button:hover:not(:disabled) {
    background: #3a3935;
  }

  button:active:not(:disabled) {
    transform: translateY(1px);
  }

  button[aria-busy="true"] {
    cursor: wait;
    opacity: 0.65;
  }

  .stack {
    display: flex;
    flex-direction: column;
    min-width: 0;
    gap: var(--space-3);
  }

  .stack > * {
    min-width: 0;
  }

  .stack > h2 {
    margin-top: var(--space-4);
  }

  .flow {
    display: flex;
    flex-flow: row wrap;
    align-items: center;
    min-width: 0;
    gap: var(--space-2);
  }

  .form {
    display: contents;
  }

  @media (max-width: 32rem) {
    main {
      padding-inline: var(--space-3);
    }

    .runtime-errors {
      padding-inline: var(--space-3);
    }

  }
CSS

Instance Method Summary collapse

Instance Method Details

#clear_action_errorObject



377
378
379
380
381
382
383
# File 'lib/sandals/html_renderer.rb', line 377

def clear_action_error
  <<~HTML
    <turbo-stream action="update" target="sandals-errors">
      <template></template>
    </turbo-stream>
  HTML
end

#render(application, view, revision:, development_reload: false) ⇒ Object



353
354
355
356
357
358
359
360
361
362
# File 'lib/sandals/html_renderer.rb', line 353

def render(application, view, revision:, development_reload: false)
  document(
    application.title,
    render_children(view),
    revision:,
    refresh_interval: application.refresh_interval,
    development_reload:,
    app_styles: application.styles.fetch(:app, {})
  )
end

#render_action_error(error) ⇒ Object



368
369
370
371
372
373
374
375
# File 'lib/sandals/html_renderer.rb', line 368

def render_action_error(error)
  original = error.original_error
  <<~HTML
    <turbo-stream action="update" target="sandals-errors">
      <template><section class="action-error" role="alert"><strong>Action failed for “#{escape(error.action_name)}”</strong><p>#{escape(original.class)}: #{escape(original.message)}</p><code>#{escape(error.location)}</code></section></template>
    </turbo-stream>
  HTML
end

#render_fragment(view) ⇒ Object



364
365
366
# File 'lib/sandals/html_renderer.rb', line 364

def render_fragment(view)
  render_children(view)
end