Draft for search result bookcover image space reservation - #13129
Draft for search result bookcover image space reservation#13129lbartron wants to merge 4 commits into
Conversation
|
Thanks for the PR, @lbartron! 🤖 Copilot has been assigned for an initial review. @lokesh is assigned to this PR and currently has:
Possible improvements for this PR
PR triage checklist (maintainers / Richy)
Note This comment was automatically generated by PAM, Open Library's Project AI Manager. PAM provides status visibility, performs basic project management functions, and gives actionable feedback so contributors aren't left waiting. |
There was a problem hiding this comment.
Pull request overview
Reserves layout space for cover images in list-view search results to reduce cumulative layout shift (CLS) by using per-book cover dimensions (cover_width/cover_height) from Solr, with a 2/3 fallback ratio when dimensions are missing.
Changes:
- Fetch
cover_width/cover_heightin the worksearch Solr scheme defaults. - Pass
cover_width/cover_heightthroughget_doc()so templates can access them. - Apply
aspect-ratioin CSS and (when available) set--cw/--chfrom the selected edition in the search result template.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| static/css/components/search-result-item.css | Switches cover rendering to aspect-ratio (with CSS var fallback) and height: auto for stable layout. |
| openlibrary/plugins/worksearch/schemes/works.py | Requests cover_width/cover_height from Solr as available fields and default fetched fields. |
| openlibrary/plugins/worksearch/code.py | Exposes cover_width/cover_height on the doc object returned to templates. |
| openlibrary/macros/SearchResultsWork.html | Sets per-result CSS variables (--cw/--ch) from the selected edition when available. |
| first_edition=doc.get("first_edition", None), | ||
| subtitle=doc.get("subtitle", None), | ||
| cover_edition_key=doc.get("cover_edition_key", None), | ||
| cover_width=doc.get("cover_width", None), | ||
| cover_height=doc.get("cover_height", None), |
| $ cover = get_cover_url(selected_ed) or "/static/images/icons/avatar_book-sm.png" | ||
| $ cover_w = selected_ed.get('cover_width') | ||
| $ cover_h = selected_ed.get('cover_height') | ||
| <a href="$work_edition_url"> | ||
| <img | ||
| itemprop="image" | ||
| src="$cover" | ||
| $:cond(cover_w and cover_h, 'style="--cw:%s;--ch:%s;"' % (cover_w, cover_h)) |
| .searchResultItem .bookcover img { | ||
| border-radius: var(--border-radius-thumbnail); | ||
| max-width: 100%; | ||
| max-height: 300px; | ||
| width: 175px; | ||
| height: auto; | ||
| aspect-ratio: var(--cw, 2) / var(--ch, 3); | ||
| } |
|
Hello! This pull request has been marked as stale because it has been waiting for submitter input for 7 days. If you're still working on this, please add a comment to keep it open. Otherwise, a maintainer will have to determine how to proceed including reassigning or opening the issue for others to work on after communicating with you. |
|
Hello! This pull request has been marked as stale because it has been waiting for submitter input for 7 days. If you're still working on this, please add a comment to keep it open. Otherwise, a maintainer will have to determine how to proceed including reassigning or opening the issue for others to work on after communicating with you. |
Closes #12825
This is a draft. Opening this PR early to check whether this per-book dimensions approach works before I finish tests and manual verification. Happy to make any changes that are needed.
Reserves layout space for cover images in list-view search results to prevent cumulative layout shift. I originally suggested a flat aspect ratio of 180 / 270. This PR uses the actual per-book cover_width and cover_height variables that are now available in Solr thanks to PR #12916.
When dimensions aren't available, which may be true for a large number of books due to the recentness of the added Solr fields, the CSS falls back to a 2/3 default ratio.
Technical
openlibrary/plugins/worksearch/schemes/works.py: requestcover_width/cover_heightfrom Solr viaall_fieldsanddefault_fetched_fields.openlibrary/plugins/worksearch/code.py: add both fields toget_doc()'s allow-list so they reach the template.openlibrary/macros/SearchResultsWork.html: set--cw/--chCSS properties from the selected edition's dimensions when available.static/css/components/search-result-item.css:.searchResultItem .bookcover imgnow usesaspect-ratio: var(--cw, 2) / var(--ch, 3)withheight: auto, replacing the oldmax-height: 300pxcap.Testing
Not yet done, but once approved, the following tests will be added:
get_doc(), and theWorkSolrBuilder/EditionSolrBuilderdimension properties.styleattribute is set/unset correctly.Screenshot
Will provide once the implementation is accepted or iterated on.
Stakeholders
@lokesh