Skip to content

Add live editing of pattern config in the UI - #11

Merged
lucianoratamero merged 3 commits into
mainfrom
live-config-editing
Nov 4, 2026
Merged

lucianoratamero merged 3 commits into
mainfrom
live-config-editing

Conversation

@lucianoratamero

@lucianoratamero lucianoratamero commented Nov 2, 2026 •

Copy link
Copy Markdown

Description

Adds a Storybook-like way to try out pattern config: in the "Template config" tab, Toggle live mode swaps the read-only YAML for an editor, and the preview re-renders as you type (500ms after you stop). Toggling it off goes back to the read-only YAML and the original preview.

Stacked on #10 (sync-upstream-v1.6.0). Merge that first, then retarget this PR to main.

How it works

  • RenderPatternView now also accepts POST with a config field (YAML). It renders through the same path as GET, so fragments are still wrapped in the base template and context modifiers and tags overrides still apply.
    • Input is parsed with yaml.safe_load. Invalid YAML, or a top level that isn't a mapping, returns a plain-text 400, which shows as a readable message in the preview.
    • CSRF protection stays on (unlike render_pattern_api, which is untouched because storybook-django depends on it).
    • xframe_options_sameorigin moved to dispatch so POST responses can still be framed.
  • The editor is a plain <form target="pattern-iframe">, so there's no fetch call and no YAML parsing in JS. config-editor.js handles the debounce, the toggle (aria-pressed) and Reset.
  • Hidden without a backend: on page load, the JS sends an OPTIONS request to the render URL. The toggle only appears if the response has the X-Pattern-Library-Live-Edit header. We can't rely on Allow: POST, because Apache lists POST for static files too.
  • iframe.js: the size label's resize listener is re-attached on each iframe load, since every navigation (including live re-renders) gets a new window.
  • The base template's pattern_config is no longer affected by rendering: render_pattern mutates the config's context in place, so the view now passes it a deep copy.

Resizable sidebar and preview (second commit)

  • Drag handles on the sidebar's right edge and in the gap below the preview. Both are focusable role="separator" elements, so the arrow keys work too.
  • They set --sidebar-width / --iframe-height. The values are saved in localStorage and restored while the <head> script runs, so the sizes carry over between pattern pages without a visible jump.
  • The sidebar stays between 150px and half the viewport width; the preview is at least 100px tall. The sidebar handle is hidden when the menu is closed or below 600px.
  • During a drag, the iframe gets pointer-events: none and the sidebar's grid transition is turned off, so the drag doesn't stall over the preview.
  • The preview's native resize grip is now horizontal only, so it doesn't fight the new height handle. The S/M/L/Full buttons are unchanged.

Known limitations

  • The preview's scroll position resets on each re-render, because the whole document reloads.
  • Edits aren't saved anywhere. They're lost when you leave live mode or reload the page.

Testing

  • python runtests.py: 53 tests pass. New tests cover POST rendering (context, tags, posted name in the base template, X-Frame-Options: SAMEORIGIN), invalid YAML (400), a missing CSRF token (403), the OPTIONS marker header, the editor markup on the index page, and pattern_config not being mutated.
  • black, isort, flake8, prettier and stylelint are clean. npm run build succeeds.
  • Browser checks (headless Chromium) against runserver: off by default, toggle on/off/on, debounce (no POST before 500ms, then one), invalid YAML message, Reset and Render buttons, a page-level pattern, the size label after a re-render, and no JS errors.
  • Static export, mirrored the same way as CI's wget --mirror from tests.settings.production: served by Python's http.server (OPTIONS → 501) and by Apache (OPTIONS → 200 with Allow: …POST…). In both, the toggle stays hidden and the read-only YAML shows, for a DTL fragment, a page pattern and a Jinja pattern. GitHub Pages answers OPTIONS with 405, so the toggle is hidden there too.
  • Resizing (browser checks, on both runserver and the static export): dragging, including across the iframe; the tabs move with the preview; sizes persist across a reload and a different pattern; arrow keys; min clamps; S/M/L still work; handle hidden with the menu closed and on mobile; no JS errors.

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@lucianoratamero
lucianoratamero added this pull request to stack #12 November 2, 2026 17:01
@lucianoratamero lucianoratamero self-assigned this Nov 2, 2026

@dchukhin dchukhin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took a look, and it works pretty well. good job!
It's pretty cool to see the static export too.

Here are a few bugs I ran into:

A. server error while typing

  1. run the server (django-admin runserver --settings=tests.settings.dev --pythonpath=.)
  2. go to /pattern/patterns/molecules/button/button.html
  3. click "Toggle live mode"
  4. In the textarea, select all and type "context: t"
  5. observe the unhappy state, and a traceback in the terminal that is running the Django server

B. empty text renders default

  1. run the server (django-admin runserver --settings=tests.settings.dev --pythonpath=.)
  2. go to /pattern/patterns/molecules/button/button.html
  3. click "Toggle live mode"
  4. Make some changes, and observe them. Then, delete everything
  5. observe that the default renders, even though it is not now shown in the textarea

C. Another behavior I observed is that making edits, then clicking the browser's back button goes back to rendering the previous edit (not the previous page), while the editable textarea does not change. I'm not sure what the correct behavior is, so I'm pointing it out here.

Base automatically changed from sync-upstream-v1.6.0 to main November 3, 2026 18:43
"Toggle live mode" in the "Template config" tab swaps the read-only YAML
for an editor. Edits re-render the preview 500ms after typing stops, via
a CSRF-protected POST to the render-pattern view, which keeps wrapping
fragments in the base template. Toggling off restores the original.

The toggle only appears when the backend answers an OPTIONS probe with
X-Pattern-Library-Live-Edit, so static exports of the UI (GitHub Pages,
Apache, etc.) keep the read-only view.
Drag handles on the sidebar's right edge and below the preview (also
keyboard-operable separators) set --sidebar-width / --iframe-height.
Sizes are kept in localStorage and restored before the body is parsed,
so they carry across pattern pages without a layout jump.

The preview's native resize grip is now horizontal-only, leaving height
to the new handle.
Comment thread pattern_library/views.py Fixed
- Validate that context/tags are mappings; a scalar (e.g. "context: t")
  returned a 500 traceback when rendering tried to index it, now 400.
- An emptied editor POSTed {}, which render_pattern treated as "no config"
  and fell back to the YAML default; only a missing config (None) should.
- Render live edits into the iframe via fetch + document.write instead of a
  form submission targeting it, so edits no longer pile up in browser history.
- Add aria-valuenow to the resize separators (role="separator" requires it)
  and keep it current as they resize.
- Stop echoing the YAML exception in the 400 response (CodeQL: information
  exposure through an exception).

@dchukhin dchukhin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to work ✔️

@lucianoratamero
lucianoratamero merged commit ae09d14 into main Nov 4, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants