puck icon indicating copy to clipboard operation
puck copied to clipboard

Add parent data to resolve data params

Open FedericoBonel opened this issue 1 month ago • 2 comments

Closes #1359.

This PR adds parent data to resolveData, matching the behavior of resolveFields as required in #1359.

Changes made

  • The resolveData config type now includes a parent.
    • The app store resolveComponentData was updated to retrieve the parent data from state and pass it to the lib resolveComponentData function.
    • The lib resolveComponentData was updated to receive the parent data from its caller and pass it to the config-defined function.
    • The lib resolveAllData was updated to pass parent data on each recursive call to the config-defined function.
      • resolveAllData now fully iterates top to bottom. Previously, this was done for slots but not for zones, which made efficient parent resolution difficult. A new utility, groupZonesByComponent, was introduced to handle this.
      • resolveAllData was also updated to fix a bug where unresolved zones promises were returned in the data.
      • resolveAllData now triggers all child resolveData calls in parallel, for both zones and slots.
  • The resolveData config type now includes a moved trigger, and the store's resolveComponentData now runs when components are moved between slots or dropzones.
    • The resolveComponentData cache now stores the parent ID for each execution and re-executes when it changes (for moved triggers).
    • A new hook, useResolveDataOnMoved, was added to track component drops and trigger resolveComponentData with a moved trigger. The cache ensures it only runs if the parent ID changed.
  • Added documentation for the new parent param, moved trigger, and updated execution flow.
  • Added tests for all new behavior.

Manual tests

  • Confirmed that resolveData runs the same number of times on editor load, insertion, deletion, replacement, and reordering within the same slot, using sample data and config.
  • Confirmed that resolveData runs once per component when a component is reordered across slots (i.e. its parent changes).
  • Confirmed that conditional data resolution based on the parent works as expected:
resolveData: (data, params) => {
  if (params.parent.type === "Grid") {
    return { props: { title: "Hello from Grid child" } };
  }
  return data;
}

https://github.com/user-attachments/assets/f1d9eaca-4a71-4f42-ab2c-a4a23075b281

FedericoBonel avatar Oct 30 '25 06:10 FedericoBonel

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
puck-demo Ready Ready Preview Comment Nov 11, 2025 3:55am
puck-docs Ready Ready Preview Comment Nov 11, 2025 3:55am

vercel[bot] avatar Oct 30 '25 06:10 vercel[bot]

Applied the requested changes, only need to go through the commit history to fix it up with you!

FedericoBonel avatar Nov 11 '25 03:11 FedericoBonel