Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set props through style plugin #6599

Merged
merged 11 commits into from
Nov 11, 2024
Merged

Conversation

bkrmendy
Copy link
Contributor

@bkrmendy bkrmendy commented Oct 30, 2024

See #6574 for a long-form writeup on the full background of this PR.

The problem addressed on this PR

The normalisation step cannot easily handle commands/actions removing properties from elements.

Fix

Instead of working around the limitation of the normalisation step, this PR introduces an approach where the style plugins themselves implement setting/removing style props on elements, and the SetProperty / DeleteProperty command uses the style plugin API to set/remove style props. The PR deals with patching props that are removed during a canvas interaction (details in #6574, TLDR: if gap is removed with the gap control, it needs to be set to 0px while the interaction is in progress, otherwise the value from the Tailwind class will be applied which would be janky).

This is an under-the-hood PR, no user-facing functionality is added.

Details

  • Updated the StylePlugin interface with the updateStyles function, which can be used to set/delete element styles. The inline style plugin and the Tailwind style plugin are updated accordingly
  • Injected StylePlugin.updateStyles into runSetProperty and runDeleteProperties via the new runStyleUpdateForStrategy function. This function encapsulates a performance-related feature: if an interaction is ongoing, it sets props in the inline style prop (and patches removed props), so updates are real-time, and when an interaction is done, it uses the active plugin. This trick works because the active strategy is re-run on interaction finish, producing the right patches for a style update.
  • The machinery dealing with zeroing props is added. The entrypoint for adding the zeroed props is patchRemovedProperties (called from dispatch-strategies.tsx, and the entrypoint for recording which props need to be zeroed is runStyleUpdateMidInteraction
  • The code related to the normalisation step is removed
  • UpdateClassListCommand is removed and turned into a helper function (since it's not called as a command anymore, just as a helper in the Tailwind style plugin)

Manual Tests:
I hereby swear that:

  • I opened a hydrogen project and it loaded
  • I could navigate to various routes in Play mode

Copy link
Contributor

github-actions bot commented Oct 30, 2024

Try me

Copy link

relativeci bot commented Oct 30, 2024

#15079 Bundle Size — 58.06MiB (~-0.01%).

badf4e9(current) vs d1d46c1 master#15078(baseline)

Warning

Bundle contains 70 duplicate packages – View duplicate packages

Bundle metrics  Change 2 changes Improvement 1 improvement
                 Current
#15079
     Baseline
#15078
Improvement  Initial JS 41.04MiB(~-0.01%) 41.04MiB
No change  Initial CSS 0B 0B
Change  Cache Invalidation 18.11% 18%
No change  Chunks 20 20
No change  Assets 22 22
No change  Modules 4167 4167
No change  Duplicate Modules 213 213
No change  Duplicate Code 27.3% 27.3%
No change  Packages 477 477
No change  Duplicate Packages 70 70
Bundle size by type  Change 2 changes Improvement 2 improvements
                 Current
#15079
     Baseline
#15078
Improvement  JS 58.06MiB (~-0.01%) 58.06MiB
Improvement  HTML 7.37KiB (-0.25%) 7.39KiB

Bundle analysis reportBranch feature/set-prop-through-pluginProject dashboard


Generated by RelativeCIDocumentationReport issue

@bkrmendy bkrmendy marked this pull request as ready for review November 6, 2024 15:04
@@ -1471,6 +1472,7 @@ export interface EditorState {
collaborators: Collaborator[]
sharingDialogOpen: boolean
editorRemixConfig: EditorRemixConfig
propertiesUpdatedDuringInteraction: UpdatedProperties
Copy link
Contributor

Choose a reason for hiding this comment

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

isn't this leftover?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's very much not a leftover, this prop is read by these functions

export function resetUpdatedProperties(editorState: EditorState): EditorState {
return { ...editorState, propertiesUpdatedDuringInteraction: {} }
}
function getPropertiesUpdatedDuringInteraction(editorState: EditorState) {
return editorState.propertiesUpdatedDuringInteraction
}
and written to here
return {
editorStateWithChanges: editorStateWithChanges,
editorStatePatches: [
editorStatePatch,
{ propertiesUpdatedDuringInteraction: { $set: updatedProperties } },
],
}

@balazsbajorics
Copy link
Contributor

The machinery dealing with zeroing props is added. The entrypoint for adding the zeroed props is patchRemovedProperties (called from dispatch-strategies.tsx, and the entrypoint for recording which props need to be zeroed is runStyleUpdateMidInteraction

why doesn't the runStyleUpdateMidInteraction inject the zeroing? feels like it is a leftover idea to run it in a later part of the update flow – IIUC the style plugin has all the information and all the power necessary to internally deal with adding these changes

@bkrmendy
Copy link
Contributor Author

The machinery dealing with zeroing props is added. The entrypoint for adding the zeroed props is patchRemovedProperties (called from dispatch-strategies.tsx, and the entrypoint for recording which props need to be zeroed is runStyleUpdateMidInteraction

why doesn't the runStyleUpdateMidInteraction inject the zeroing? feels like it is a leftover idea to run it in a later part of the update flow – IIUC the style plugin has all the information and all the power necessary to internally deal with adding these changes

We talked about this IRL, the reason this is needed is because the code that does the zeroing for padding needs to know about all the props that have been set/unset (see this code from the spike)

@bkrmendy bkrmendy merged commit 07b81de into master Nov 11, 2024
14 checks passed
@bkrmendy bkrmendy deleted the feature/set-prop-through-plugin branch November 11, 2024 15:31
liady pushed a commit that referenced this pull request Dec 13, 2024
See #6574 for a long-form
writeup on the full background of this PR.

## The problem addressed on this PR
The normalisation step cannot easily handle commands/actions removing
properties from elements.

## Fix
Instead of working around the limitation of the normalisation step, this
PR introduces an approach where the style plugins themselves implement
setting/removing style props on elements, and the `SetProperty` /
`DeleteProperty` command uses the style plugin API to set/remove style
props. The PR deals with patching props that are removed during a canvas
interaction (details in
#6574, TLDR: if `gap` is
removed with the gap control, it needs to be set to `0px` while the
interaction is in progress, otherwise the value from the Tailwind class
will be applied which would be janky).

This is an under-the-hood PR, no user-facing functionality is added.

### Details
- Updated the `StylePlugin` interface with the `updateStyles` function,
which can be used to set/delete element styles. The inline style plugin
and the Tailwind style plugin are updated accordingly
- Injected `StylePlugin.updateStyles` into `runSetProperty` and
`runDeleteProperties` via the new `runStyleUpdateForStrategy` function.
This function encapsulates a performance-related feature: if an
interaction is ongoing, it sets props in the inline style prop (and
patches removed props), so updates are real-time, and when an
interaction is done, it uses the active plugin. This trick works because
the active strategy is re-run on interaction finish, producing the right
patches for a style update.
- The machinery dealing with zeroing props is added. The entrypoint for
adding the zeroed props is `patchRemovedProperties` (called from
`dispatch-strategies.tsx`, and the entrypoint for recording which props
need to be zeroed is `runStyleUpdateMidInteraction`
- The code related to the normalisation step is removed
- `UpdateClassListCommand` is removed and turned into a helper function
(since it's not called as a command anymore, just as a helper in the
Tailwind style plugin)

**Manual Tests:**
I hereby swear that:

- [x] I opened a hydrogen project and it loaded
- [x] I could navigate to various routes in Play mode
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.

4 participants