oficina / estilo Public

Clone
git clone https://amadan.net/oficina/estilo

Plain git — no account needed to clone.

Download

Download this file

1// Package side is an app's side panel ([estilo-side]) and the frame that
2// puts it under the bar, beside <main> ([estilo-side-frame]). Its rows
3// ([estilo-side-item]) are an icon and a label ([estilo-side-label]) with
4// an optional shortcut ([estilo-side-key]); page rows sit under section
5// headings ([estilo-side-head]), and the foot ([estilo-side-foot]) is
6// pinned to the bottom with the fold, which turns the side into a rail of
7// icons and back.
8//
9// Mapa built it (its level-up spec, §3.2) and Contra is the second app to
10// use it, so it moved here when Contra adopted it (AGENTS.md, decisions
11// table). The values and the behaviour are Mapa's, copied; changing them
12// is a design decision.
13//
14// Widths: from 80rem the side is open by default and below it a rail;
15// the State (a cookie the app keeps) overrides that from 800px. Below
16// 800px there is no side: its places belong in the bar's Menu sheet
17// (chrome.Bar.Nav), and the bar's own nav is hidden from 800px on a page
18// with a frame, because the side carries the places there.
19//
20// Script line: everything works with scripts off. Rows are links; the
21// fold is a form that POSTs the new state (SetState stores it), and the
22// frame draws it on the next page. JS() only folds in place, with the
23// same cookie, and adds ⌘\ (Ctrl+\ elsewhere).
24//
25// It carries no strings. Callers supply every label in their locale.
26package side
27
28import (
29 "embed"
30 "html/template"
31 "net/http"
32)
33
34//go:embed side.css
35var css []byte
36
37//go:embed side.js
38var js []byte
39
40//go:embed templates/*.html
41var templateFS embed.FS
42
43// CSS returns side.css. Link it after chrome.css: the side's private
44// notes use chrome's [estilo-visually-hidden], and it sizes itself by
45// chrome's --estilo-bar-block-size.
46func CSS() []byte { return append([]byte(nil), css...) }
47
48// JS returns side.js, an optional classic script (defer) that folds the
49// side in place: any control with data-estilo-side-fold (the fold's own
50// buttons, or an app's menu row) and ⌘\ or Ctrl+\ switch the frame
51// between open and rail and set the fold's cookie. Focus stays on
52// screen: a fold button hands it to the other one, a menu row to its
53// menu's title, and a row the rail hides to the lit place (or the
54// fold). Escape dismisses a rail tooltip until the next hover or focus.
55// A page with no frame or no Fold has nothing to fold, and the script
56// leaves the shortcut alone. Without it the fold posts and the next page
57// is drawn folded.
58func JS() []byte { return append([]byte(nil), js...) }
59
60// Templates holds estilo-side, estilo-side-item, estilo-side-fold and the
61// side's two icons (estilo-icon-panel-left, estilo-icon-lock). Parse it
62// into the app's page templates with ParseFS(Templates(), "templates/*.html").
63func Templates() embed.FS { return templateFS }
64
65// Side is estilo-side's data.
66//
67// The frame is the app's own markup, around the side and <main>:
68//
69// <div estilo-side-frame="{{.State}}">
70// {{template "estilo-side" .}}
71// <main id="main">…</main>
72// </div>
73type Side struct {
74 // Label is the side's accessible name: the app's name ("Mapa").
75 Label string
76 // State is what the side shows: "open", "rail", or "" to let the
77 // width decide (open from 80rem). Write it as the frame's
78 // estilo-side-frame value. Read it with State.
79 State string
80 // Places are the app's sections. They stay on the rail, as icons.
81 Places []Item
82 // Sections are lists of page rows under a heading ("Pinned",
83 // "Recently edited"). The rail hides them. A section with no items
84 // draws nothing, heading included.
85 Sections []Section
86 // Foot is app markup drawn at the top of the foot, before the fold
87 // (Mapa's New team page): estilo-side-item rows the app rendered
88 // itself, with estilo-side-label around each label so the rail can
89 // make it the tooltip. Trusted markup, never user input.
90 Foot template.HTML
91 // Fold is the fold to a rail; nil leaves the side always as the
92 // width draws it.
93 Fold *Fold
94}
95
96// Section is one heading and its page rows.
97type Section struct {
98 Heading string
99 Items []Item
100}
101
102// Item is one row.
103type Item struct {
104 Href, Label string
105 // Icon is the row's glyph: the app's own inline <svg
106 // aria-hidden="true">, trusted markup. Every row has one; on the rail
107 // it is all that shows.
108 Icon template.HTML
109 // Key is an already-formatted shortcut (menu.Shortcut), shown at the
110 // row's end in the open side (Mapa's Search, ⌘K). Optional.
111 Key string
112 // Current marks the row for the page on screen (aria-current="page").
113 // Call Light before rendering so exactly one row carries it.
114 Current bool
115 // Holds is set by Light on a place that holds the page on screen when
116 // that page has its own row: the place is drawn twice, unlit for the
117 // open side and lit for the rail, which hides the page rows.
118 Holds bool
119 // Locked, when set, puts a lock at the row's end (Mapa: a private
120 // note of your own) and is said after the label (", only you see
121 // this").
122 Locked string
123}
124
125// Fold is the side's fold: a form that POSTs state=rail or state=open
126// to Action, with Return as "return", for the app to store with SetState
127// and redirect. Both buttons are drawn and CSS shows the one that fits
128// what is on screen, so it works with scripts off and with no cookie.
129type Fold struct {
130 Action, Return string
131 // Hide and Show are the two buttons' labels ("Hide sidebar", "Show
132 // sidebar").
133 Hide, Show string
134 // Key is the shortcut shown on Hide: menu.Shortcut(r, "Mod+\\").
135 // side.js binds ⌘\ and Ctrl+\ whether or not it is shown.
136 Key string
137 // Cookie is the cookie that holds the state (Mapa's "mapa-sidebar"),
138 // for side.js to set when it folds in place. The same name goes to
139 // State and SetState.
140 Cookie string
141}
142
143// Light leaves exactly one full highlight (Mapa, Paul 2026-11-16): on the
144// page's own row when it has one, the first current row of the
145// Sections, and otherwise on its place. When a page row is lit, a place
146// that was current becomes Holds instead: drawn twice, unlit while the
147// side is open and lit on the rail, which hides the page rows. The
148// state, the width and a fold in place all choose between them by CSS
149// alone.
150func (s *Side) Light() {
151 lit := false
152 for i := range s.Sections {
153 items := s.Sections[i].Items
154 for j := range items {
155 if items[j].Current && lit {
156 items[j].Current = false
157 }
158 if items[j].Current {
159 lit = true
160 }
161 }
162 }
163 if !lit {
164 return
165 }
166 for i := range s.Places {
167 place := &s.Places[i]
168 place.Holds, place.Current = place.Current, false
169 }
170}
171
172// cookieYear is how long a fold is kept.
173const cookieYear = 365 * 24 * 60 * 60
174
175// State is the side's state from the request's cookie: "open", "rail",
176// or "" when it holds nothing known, which lets the width decide.
177func State(r *http.Request, cookie string) string {
178 c, err := r.Cookie(cookie)
179 if err != nil {
180 return ""
181 }
182 switch c.Value {
183 case "open", "rail":
184 return c.Value
185 }
186 return ""
187}
188
189// SetState is the fold's POST handler's work: it reads the form's
190// "state", or, without a known one, toggles from "shown" (what the page
191// showed), stores it in the cookie for a year and returns it. A fold is
192// a view preference, not a write. The app redirects, to the form's
193// "return" once it has checked that it is a path on its own site.
194func SetState(w http.ResponseWriter, r *http.Request, cookie string) string {
195 state := r.PostFormValue("state")
196 if state != "open" && state != "rail" {
197 state = "rail"
198 if r.PostFormValue("shown") == "rail" {
199 state = "open"
200 }
201 }
202 http.SetCookie(w, &http.Cookie{Name: cookie, Value: state, Path: "/",
203 MaxAge: cookieYear, SameSite: http.SameSiteLaxMode, Secure: true})
204 return state
205}
206