From e7047e2aeae6e493075b6bf23fc7687681dfedce Mon Sep 17 00:00:00 2001 From: ljkx <177671075+ljkx@users.noreply.github.com> Date: Mon, 23 Nov 2026 10:37:20 +0100 Subject: [PATCH] M3: Swap library re-points the styles and variables of the copies it keeps (#2568) library.swap re-pointed style links and variable bindings only on layers outside the read-only library copies. A copy the swap keeps (no match in the other library, or another library's component using the swapped style) kept the old style and variable, so instances placed after the swap showed the old colours. The copies' own layers are re-pointed too. --- crates/engine/src/assets/library_swap.rs | 6 +- .../engine/src/assets/library_swap/tests.rs | 70 ++++++++++++++++++- docs/components-parity.md | 5 +- 3 files changed, 76 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/assets/library_swap.rs b/crates/engine/src/assets/library_swap.rs index e3b312cf8..e6071e0de 100644 --- a/crates/engine/src/assets/library_swap.rs +++ b/crates/engine/src/assets/library_swap.rs @@ -210,8 +210,10 @@ pub fn swap_library(doc: &mut Document, from: &str, to: &PublishedLibrary) -> Re out.unmatched.push(format!("extended collection {}", c.name)); } - // Re-point layers outside the library copies, and local variables aliasing A's. - let ids: Vec = doc.nodes().filter(|n| outside(doc, n.id)).map(|n| n.id).collect(); + // Re-point the layers, and local variables aliasing A's. The library copies' own layers too: + // a copy B has no match for stays (A's, or another library's), and instances placed from it + // later must link what the ones re-pointed here link (#2568). + let ids: Vec = doc.nodes().map(|n| n.id).collect(); for id in ids { let Ok(n) = doc.get_mut(id) else { continue }; for r in [&mut n.styles.fill, &mut n.styles.stroke, &mut n.styles.effect, &mut n.styles.grid, &mut n.styles.text] { diff --git a/crates/engine/src/assets/library_swap/tests.rs b/crates/engine/src/assets/library_swap/tests.rs index d90996576..a132e4610 100644 --- a/crates/engine/src/assets/library_swap/tests.rs +++ b/crates/engine/src/assets/library_swap/tests.rs @@ -31,6 +31,12 @@ impl Drop for Scratch { /// A library file at `path` with "Button" (filled `color`, a "Label" text), a "Brand" paint style, /// a "Theme" collection with an `accent` colour, and `extra` components; returns its id. fn library(path: &str, color: &str, extra: &[&str]) -> String { + library_with(path, color, extra, false) +} + +/// [`library`], and with `linked` also "Badge", filled by the Brand style, and "Dot", its fill +/// bound to `accent`: components whose own layers link the library's style and variable. +fn library_with(path: &str, color: &str, extra: &[&str], linked: bool) -> String { let mut s = Session::new(); let f = idv(&ex(&mut s, "frame.create", json!({"x": 0, "y": 0, "width": 100, "height": 40, "name": "Button"}))); ex(&mut s, "text.create", json!({"text": "OK", "x": 10, "y": 10, "parent": f, "props": {"name": "Label"}})); @@ -41,6 +47,14 @@ fn library(path: &str, color: &str, extra: &[&str]) -> String { ex(&mut s, "style.create", json!({"type": "paint", "name": "Brand", "from": r})); ex(&mut s, "variable.createCollection", json!({"name": "Theme", "modes": ["Light", "Dark"]})); ex(&mut s, "variable.create", json!({"collection": "Theme", "name": "accent", "type": "color", "value": color})); + if linked { + let badge = idv(&ex(&mut s, "frame.create", json!({"x": 0, "y": 300, "width": 30, "height": 30, "name": "Badge"}))); + ex(&mut s, "style.apply", json!({"ids": [badge], "style": "Brand"})); + let dot = idv(&ex(&mut s, "frame.create", json!({"x": 50, "y": 300, "width": 30, "height": 30, "name": "Dot"}))); + ex(&mut s, "variable.bind", json!({"ids": [dot], "field": "fill", "variable": "accent"})); + ex(&mut s, "component.create", json!({"ids": [badge]})); + ex(&mut s, "component.create", json!({"ids": [dot]})); + } for (i, name) in extra.iter().enumerate() { let e = idv(&ex(&mut s, "frame.create", json!({"x": 200 + 50 * i, "y": 0, "width": 20, "height": 20, "name": name}))); ex(&mut s, "component.create", json!({"ids": [e]})); @@ -60,10 +74,15 @@ fn main_of(s: &Session, inst: NodeId) -> NodeId { /// A session with libraries A (blue, plus `extra_a` components) and B (green) enabled, files in a /// scratch directory named by `tag`; returns the directory guard, the session and both library ids. fn two_libraries(tag: &str, extra_a: &[&str]) -> (Scratch, Session, String, String) { + two_libraries_with(tag, extra_a, false) +} + +/// [`two_libraries`], with A made by [`library_with`] and `linked_a`. +fn two_libraries_with(tag: &str, extra_a: &[&str], linked_a: bool) -> (Scratch, Session, String, String) { let dir = Scratch(std::env::temp_dir().join(format!("figcraft-libswap-{tag}-{}", std::process::id()))); std::fs::create_dir_all(&dir.0).unwrap(); let (pa, pb) = (dir.0.join("a.figcraft").to_string_lossy().into_owned(), dir.0.join("b.figcraft").to_string_lossy().into_owned()); - let a = library(&pa, "#0000ff", extra_a); + let a = library_with(&pa, "#0000ff", extra_a, linked_a); let b = library(&pb, "#00ff00", &[]); let mut s = Session::new(); ex(&mut s, "library.add", json!({"path": pa})); @@ -180,3 +199,52 @@ fn swap_repoints_colour_calculations() { let reads: Vec<&VariableId> = ["veil", "wash"].iter().flat_map(|n| vars.get(n).unwrap().values.values()).flat_map(|v| v.references()).collect(); assert!(reads.iter().all(|r| doc.libraries.variable_import(r).is_some_and(|i| i.library == b)), "{reads:?}"); } + +/// A component B has no match for stays on A, but the Brand style and `accent` variable its own +/// layers link do have matches (#2568). The swap re-points them in A's read-only copy as on the +/// placed instances, so an instance placed after the swap looks like the ones placed before it, +/// and A's style and variable copies nothing links any more go. +#[test] +fn swap_repoints_the_styles_and_variables_of_component_copies_it_keeps() { + let (_dir, mut s, a, b) = two_libraries_with("copies", &[], true); + let badge = idv(&ex(&mut s, "library.place", json!({"library": a, "component": "Badge", "x": 0, "y": 0}))); + let dot = idv(&ex(&mut s, "library.place", json!({"library": a, "component": "Dot", "x": 100, "y": 0}))); + let fill = |s: &Session, id: NodeId| s.doc().unwrap().node(id).unwrap().fills[0].solid_color().unwrap().to_hex(); + assert_eq!((fill(&s, badge), fill(&s, dot)), ("0000FF".to_string(), "0000FF".to_string())); + + let r = ex(&mut s, "library.swap", json!({"from": a, "to": b})); + assert_eq!((r["components"].as_u64(), r["styles"].as_u64(), r["variables"].as_u64()), (Some(0), Some(1), Some(1)), "{r}"); + assert_eq!(r["unmatched"], json!(["component Badge", "component Dot"]), "{r}"); + let (badge_main, dot_main) = (main_of(&s, badge), main_of(&s, dot)); + let doc = s.doc().unwrap(); + assert_eq!((library_of(doc, badge_main), library_of(doc, dot_main)), (Some(a.as_str()), Some(a.as_str())), "no match: still A's"); + // The copies link B's style and variable, as the instances do. + for (id, what) in [(badge_main, "the Badge copy"), (badge, "the Badge instance")] { + let style = doc.node(id).unwrap().styles.fill.clone().unwrap_or_default(); + assert!(doc.libraries.style_import(&style).is_some_and(|i| i.library == b), "{what} links {style}"); + } + for (id, what) in [(dot_main, "the Dot copy"), (dot, "the Dot instance")] { + let bound = doc.node(id).unwrap().bound.get("fill").cloned().unwrap_or_default(); + assert!(doc.libraries.variable_import(&bound).is_some_and(|i| i.library == b), "{what} binds {bound}"); + } + assert!(doc.libraries.styles.iter().all(|i| i.library == b), "A's Brand copy is linked by nothing: {:?}", doc.libraries.styles); + assert!(doc.libraries.variables.iter().all(|i| i.library == b), "A's accent copy is bound by nothing: {:?}", doc.libraries.variables); + assert!(doc.validate().is_empty(), "{:?}", doc.validate()); + + // Instances placed after the swap show B's colours, as those placed before it. + let late_badge = idv(&ex(&mut s, "instance.create", json!({"component": badge_main, "x": 0, "y": 100}))); + let late_dot = idv(&ex(&mut s, "instance.create", json!({"component": dot_main, "x": 100, "y": 100}))); + let shown: Vec = [badge, dot, late_badge, late_dot].iter().map(|i| fill(&s, *i)).collect(); + assert_eq!(shown, ["00FF00"; 4]); + + // One undo of each instance and the swap puts the copies back on A's style and variable. + s.undo().unwrap(); + s.undo().unwrap(); + s.undo().unwrap(); + let doc = s.doc().unwrap(); + let style = doc.node(badge_main).unwrap().styles.fill.clone().unwrap_or_default(); + assert!(doc.libraries.style_import(&style).is_some_and(|i| i.library == a), "{style}"); + let bound = doc.node(dot_main).unwrap().bound.get("fill").cloned().unwrap_or_default(); + assert!(doc.libraries.variable_import(&bound).is_some_and(|i| i.library == a), "{bound}"); + assert_eq!((fill(&s, badge), fill(&s, dot)), ("0000FF".to_string(), "0000FF".to_string())); +} diff --git a/docs/components-parity.md b/docs/components-parity.md index fa4d2494e..2ca1df0e7 100644 --- a/docs/components-parity.md +++ b/docs/components-parity.md @@ -1,6 +1,6 @@ # Components, variants, styles and variables parity -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-11-19 · **Change:** minor (a style of one library that reaches a file through another library's component is, in a file linking both, the first library's own bound copy, so its modes colour it (#2414); with a variant selected, its property dropdowns show its own values and choosing one renames only that variant, instead of setting the set's default and moving the variants (#2349); library analytics count insertions and detachments of styles and variables; combining components gives every variant the properties any of them had; renaming a variable group to a path with `/` nests it under a parent group instead of turning the slash into a hyphen (#2353); a group's context menu in the Local variables window has Duplicate group, a copy named after it plus " 2" holding a copy of every variable, in one undo step (#2084); pages take a variable mode: with nothing selected the Design panel's Page section shows a Variable modes section, Auto or a mode of each collection that has more than one, as the help center says ("Apply variable mode" in the Page section of the right sidebar) (#2215); a text style binds a number variable to its weight (#2174); an instance lists only the properties its variant's layers use and long property names are cut to one line (#2022); with several layers selected the Design panel header's component button offers Create multiple components (#2088); the Local variables window selects several variables (click, Shift+click for a range, Cmd/Ctrl+click to toggle, Escape to clear) and delete, move to a group and scope act on the selected local variables in one undo step (#2002); text styles carry paragraph spacing and indent, the list style and the OpenType features, as the Supported in text styles table of Create and apply text styles says (#1829); a layer's effect binding takes only a field its effect has; Go to main component on an instance of a library component opens the library file in a tab and selects the main there; glass effects take variables; Delete on a layer inside an instance hides it as an override instead of being refused, as the help center says: "Figma will only toggle the layer's visibility instead of removing it"; library analytics list how many layers and files use each style and variable; SwiftUI and Compose code names a bound variable by its iOS or Android code syntax; library analytics count insertions and show insertions and detachments over the past 30 days, 90 days or year; a collection of this file can be extended (Extend collection on its row, `variable.extendCollection`), its extension following its edits at once, and extended collections extend again in the file; a typed or stored variable, collection or mode id names only the object with that id (#1613); colour variables can be calculated from other colours per mode — `{alpha}` replaces a colour's alpha, `{mix}` blends two — and follow them (#1280); stored variable and collection references keep their identity when another object is named like their id (follow-up to #1542); a component set's property rows fit the 240 px right panel: variant values wrap two to a line there and every + and − stays in view; opening a file contacts no team library server it names until you check it (#1097); an explicit variables file path (`variable.import`, the `variables` of `file.open` and `file.import`) is refused past 256 MiB or when it isn't a regular file, as the sibling `variables.json` already was (part of #1114); component properties keep the order they were made in when one is renamed, and Make slot asks for the slot's name first (#1536, #1537); components can't contain their own instances, directly or through others (#1084); the instance-swap menus have a search field (#1338); an instance-swap property's default menu scrolls (#1338); variables on a text style's fields from the style editor; instance-swap properties in the component playground; duplicating or Alt-dragging a main component makes an instance of it (#892); a bound colour variable's alpha applies once (#802); variables import reads `variables` outside `meta` and warns when it imports none (#790); variant properties reorder by drag; library files saved right after a read are read again; Hide when publishing note corrected; size and resizing overrides of layers inside instances (#731); variables export in the browser as a download; core editor leftovers: component playground, variables inside styles; layers and assets panels behaviour suite and fixes; extended collections tail; styles and variables behaviour suite and fixes; components behaviour suite and fixes; team libraries on the server, library usage and swapping a library; extended collections, variables import/export, gradient stops and variant properties bound to variables) · **Target:** Figma Design (desktop app 126.10.7) +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-11-23 · **Change:** minor (Swap library re-points the styles and variables of the library copies that stay, so instances placed after a swap follow it too (#2568); a style of one library that reaches a file through another library's component is, in a file linking both, the first library's own bound copy, so its modes colour it (#2414); with a variant selected, its property dropdowns show its own values and choosing one renames only that variant, instead of setting the set's default and moving the variants (#2349); library analytics count insertions and detachments of styles and variables; combining components gives every variant the properties any of them had; renaming a variable group to a path with `/` nests it under a parent group instead of turning the slash into a hyphen (#2353); a group's context menu in the Local variables window has Duplicate group, a copy named after it plus " 2" holding a copy of every variable, in one undo step (#2084); pages take a variable mode: with nothing selected the Design panel's Page section shows a Variable modes section, Auto or a mode of each collection that has more than one, as the help center says ("Apply variable mode" in the Page section of the right sidebar) (#2215); a text style binds a number variable to its weight (#2174); an instance lists only the properties its variant's layers use and long property names are cut to one line (#2022); with several layers selected the Design panel header's component button offers Create multiple components (#2088); the Local variables window selects several variables (click, Shift+click for a range, Cmd/Ctrl+click to toggle, Escape to clear) and delete, move to a group and scope act on the selected local variables in one undo step (#2002); text styles carry paragraph spacing and indent, the list style and the OpenType features, as the Supported in text styles table of Create and apply text styles says (#1829); a layer's effect binding takes only a field its effect has; Go to main component on an instance of a library component opens the library file in a tab and selects the main there; glass effects take variables; Delete on a layer inside an instance hides it as an override instead of being refused, as the help center says: "Figma will only toggle the layer's visibility instead of removing it"; library analytics list how many layers and files use each style and variable; SwiftUI and Compose code names a bound variable by its iOS or Android code syntax; library analytics count insertions and show insertions and detachments over the past 30 days, 90 days or year; a collection of this file can be extended (Extend collection on its row, `variable.extendCollection`), its extension following its edits at once, and extended collections extend again in the file; a typed or stored variable, collection or mode id names only the object with that id (#1613); colour variables can be calculated from other colours per mode — `{alpha}` replaces a colour's alpha, `{mix}` blends two — and follow them (#1280); stored variable and collection references keep their identity when another object is named like their id (follow-up to #1542); a component set's property rows fit the 240 px right panel: variant values wrap two to a line there and every + and − stays in view; opening a file contacts no team library server it names until you check it (#1097); an explicit variables file path (`variable.import`, the `variables` of `file.open` and `file.import`) is refused past 256 MiB or when it isn't a regular file, as the sibling `variables.json` already was (part of #1114); component properties keep the order they were made in when one is renamed, and Make slot asks for the slot's name first (#1536, #1537); components can't contain their own instances, directly or through others (#1084); the instance-swap menus have a search field (#1338); an instance-swap property's default menu scrolls (#1338); variables on a text style's fields from the style editor; instance-swap properties in the component playground; duplicating or Alt-dragging a main component makes an instance of it (#892); a bound colour variable's alpha applies once (#802); variables import reads `variables` outside `meta` and warns when it imports none (#790); variant properties reorder by drag; library files saved right after a read are read again; Hide when publishing note corrected; size and resizing overrides of layers inside instances (#731); variables export in the browser as a download; core editor leftovers: component playground, variables inside styles; layers and assets panels behaviour suite and fixes; extended collections tail; styles and variables behaviour suite and fixes; components behaviour suite and fixes; team libraries on the server, library usage and swapping a library; extended collections, variables import/export, gradient stops and variant properties bound to variables) · **Target:** Figma Design (desktop app 126.10.7) Design systems are why teams adopt Figma. **Components: breadth ~96%, ready ~72%. Styles and variables: breadth ~95%, ready ~76% (estimated).** QA'd with a synthetic design system of ~300 @@ -55,7 +55,7 @@ updates with overrides kept: `library_tests.rs`, `components_scale_tests.rs`. | Library updates available; overrides kept | done | a library file saved again within two seconds of being read, at the same length, is read again (its time stamp can't prove it unchanged; #652) | | Team libraries: publish to a `figcraft-server` under the file, enable by a share link (a view link reads it), background update checks | done | [server.md](server.md#team-libraries); no team-wide library list or accounts ([gaps](gaps.md) #10) | | Library analytics | partial | instances per component and the files using them, and the instances each file detached (per component and per file), as apps report them; insertions and detachments week by week for a year (Show usage: the past 30 days, 90 days or year), for components; the layers and files using each style and variable, with their insertions and detachments over the same periods, no modes; no chart | -| Swap library (instances, styles and variables to another library's of the same names, overrides kept) | done | what has no match stays and is listed; one undo step | +| Swap library (instances, styles and variables to another library's of the same names, overrides kept) | done | what has no match stays and is listed; one undo step; a library copy that stays (no match, or another library's) links the swapped styles and variables too, so instances placed after the swap look like those placed before it (`library_swap/tests.rs`, #2568) | ## Styles and variables @@ -80,6 +80,7 @@ updates with overrides kept: `library_tests.rs`, `components_scale_tests.rs`. | Date | Change | Summary | |---|---|---| +| 2026-11-23 | patch | M3, #2568: `library.swap {from, to}` re-pointed style links and variable bindings only on layers outside the read-only library copies. A copy the swap keeps (a component of `from` that `to` has no match for, or another library's component using `from`'s style) kept `from`'s style and variable, so instances placed after the swap showed the old colours while those placed before showed the new ones. The swap now re-points the copies' own layers too, with the same maps, and `from`'s style and variable copies that nothing links any more go. Test: `library_swap/tests.rs` (`swap_repoints_the_styles_and_variables_of_component_copies_it_keeps`) | | 2026-11-19 | patch | M3, #2414: a kit's style used by a component of a second library reached a file linking both as a second, unbound copy (`::S:1`) frozen in the kit's default mode, so a mode set on the kit's collection didn't colour it (from two hops on, every such style). Publishing now keeps the variables the published styles bind and where the file's copies of other libraries' styles, variables and collections came from; importing files such a copy under that library when the file links it, so it is the file's own bound copy. Test: `library_style_hops_tests::a_kit_style_through_another_library_is_the_kits_bound_copy`; the issue's four-file chain renders Brand and Alt everywhere. Grade unchanged. | | 2026-11-19 | patch | M3, #2349: with one variant of a set selected, the Design panel's variant property dropdowns showed the set's default values, and choosing a value set the default, so the variants swapped places. They now show the selected variant's own values (hover: "This variant's value"), and choosing one renames that variant only; with the set selected they still set its default. Test: `variant_own_value_tests::a_selected_variants_dropdown_shows_and_sets_its_own_value`. Grade unchanged. | | 2026-11-18 | minor | M3, gap 10: library analytics count insertions and detachments of styles and variables. Applying one, or copying or duplicating content that uses it, inserts it; the Detach button detaches it; an instance counts as its component alone. Show usage and `library.usage` give them per style and variable over the chosen period. Modes aren't counted. Numbers unchanged. Tests: `library_asset_counts_tests`, `library_assets_tests`. |