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

Controller: @phase2/outline-controller-resize-controller #393

Open
wants to merge 7 commits into
base: next
Choose a base branch
from

Conversation

shaal
Copy link
Contributor

@shaal shaal commented May 22, 2023

Testing

Description

The new resize controller includes multiple improvements over previous attempts

This resize-controller has been extensively tested by multiple projects.

Component width is being tracked by the Browser API's resize observer

Debounce during resize to reduce amount of events

Only when component width moves over from one breakpoint range to another, component update is called.

During component render, only current breakpoint range variable is read (no function is being called)

Provides the ability to separate the element tracking width from the element that will be updated (useful when we need to synchronize all items in a container to switch together at the same breakpoint).

Fixes # (issue)

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Visual Testing
  • Automated Testing
  • Accessibility Testing

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Summary by CodeRabbit

  • New Features
    • Introduced a ResizeController utility for monitoring and reacting to size changes in web components, enabling responsive design adjustments.

@changeset-bot
Copy link

changeset-bot bot commented May 22, 2023

⚠️ No Changeset found

Latest commit: 5b3724a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@netlify
Copy link

netlify bot commented May 22, 2023

Deploy Preview for outlinejs ready!

Name Link
🔨 Latest commit 5b3724a
🔍 Latest deploy log https://app.netlify.com/sites/outlinejs/deploys/65c247cf76f0e1000864634d
😎 Deploy Preview https://deploy-preview-393--outlinejs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@shaal shaal mentioned this pull request May 22, 2023
15 tasks
@himerus himerus added Type: Controller Suggestion Use this label for any issue that is a recommendation for a specific type of controller. CANNOT MERGE Temporary flag for a PR that has failing tests, broken pipelines, etc. labels May 22, 2023
@himerus himerus removed the CANNOT MERGE Temporary flag for a PR that has failing tests, broken pipelines, etc. label May 22, 2023
@himerus himerus changed the title feat: initial resize-controller as package Controller: @phase2/outline-controller-resize-controller May 22, 2023
@himerus himerus self-assigned this May 22, 2023
Copy link

coderabbitai bot commented Feb 6, 2024

Walkthrough

The update introduces a new utility module named resize-controller, which includes a ResizeController class for monitoring and reacting to size changes in web components. This module enhances dynamic responsiveness by simplifying the handling of resize events and enabling developers to execute actions based on component size adjustments.

Changes

File Path Change Summary
.../resize-controller/ Introduces the resize-controller utility module for monitoring and reacting to web component size changes.
.../resize-controller/index.ts Exports the ResizeController class from src/resize-controller.
.../resize-controller/src/resize-controller.ts Adds ResizeController class implementing ReactiveController for observing element resize events and managing breakpoints.

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 6

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 3005e66 and b769a0c.
Files ignored due to path filters (2)
  • packages/controllers/resize-controller/package.json is excluded by: !**/*.json
  • packages/controllers/resize-controller/tsconfig.build.json is excluded by: !**/*.json
Files selected for processing (3)
  • packages/controllers/resize-controller/README.md (1 hunks)
  • packages/controllers/resize-controller/index.ts (1 hunks)
  • packages/controllers/resize-controller/src/resize-controller.ts (1 hunks)
Files skipped from review due to trivial changes (1)
  • packages/controllers/resize-controller/index.ts
Additional comments: 2
packages/controllers/resize-controller/README.md (1)
  • 29-94: API reference and example usage accurately reflect the implementation details and capabilities of the ResizeController class.
packages/controllers/resize-controller/src/resize-controller.ts (1)
  • 11-29: The debounce function implementation is correct and follows best practices for debouncing.

You can install the `resize-controller` package using yarn:

```
yarn add -D @phase2/resize-controller
Copy link

Choose a reason for hiding this comment

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

Incorrect package name in the yarn add command.

- yarn add -D @phase2/resize-controller
+ yarn add -D @phase2/outline-controller-resize-controller

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
yarn add -D @phase2/resize-controller
yarn add -D @phase2/outline-controller-resize-controller

1. Import the necessary classes and functions from the `lit` package:

```javascript
import { ResizeController } from 'resize-controller';
Copy link

Choose a reason for hiding this comment

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

Incorrect import statement, does not match the actual package name.

- import { ResizeController } from 'resize-controller';
+ import { ResizeController } from '@phase2/outline-controller-resize-controller';

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
import { ResizeController } from 'resize-controller';
import { ResizeController } from '@phase2/outline-controller-resize-controller';

*/
initializeBreakpointsRangeType() {
// This will allow create an additional breakpoint from the last custom breakpoint to 100000
this.options.breakpoints?.push(100000);
Copy link

Choose a reason for hiding this comment

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

Modifying the options object directly by pushing to breakpoints array can lead to unexpected side effects.

- this.options.breakpoints?.push(100000);
+ const extendedBreakpoints = [...this.options.breakpoints, 100000];
+ this.options = { ...this.options, breakpoints: extendedBreakpoints };

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
this.options.breakpoints?.push(100000);
const extendedBreakpoints = [...this.options.breakpoints, 100000];
this.options = { ...this.options, breakpoints: extendedBreakpoints };

Comment on lines +115 to +120
if (!this.host.style.display) {
// adding `display: block` to :host of component
this.host.style.setProperty(
'display',
'var(--style-added-by-resize-controller, block)'
);
Copy link

Choose a reason for hiding this comment

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

Setting display: block directly on the host element may not be appropriate for all components and could override existing styles.

Consider using a more flexible approach or documenting this behavior clearly to inform users.

Comment on lines +123 to +132
// Create a new ResizeObserver and pass in the function to be called when the element is resized
this.resizeObserver = new ResizeObserver(
(entries: ResizeObserverEntry[]) => {
// Create a debounced version of the onElementResize function
debounce(
this.onElementResize.bind(this),
this.options.debounce
)(entries);
}
);
Copy link

Choose a reason for hiding this comment

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

The debounced function is recreated on every resize event, which negates the purpose of debouncing.

Move the debounced function creation outside of the ResizeObserver callback to ensure it's created only once.

+ const debouncedOnElementResize = debounce(
+   this.onElementResize.bind(this),
+   this.options.debounce
+ );
this.resizeObserver = new ResizeObserver(
  (entries: ResizeObserverEntry[]) => {
-     debounce(
-       this.onElementResize.bind(this),
-       this.options.debounce
-     )(entries);
+     debouncedOnElementResize(entries);
  }
);

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
// Create a new ResizeObserver and pass in the function to be called when the element is resized
this.resizeObserver = new ResizeObserver(
(entries: ResizeObserverEntry[]) => {
// Create a debounced version of the onElementResize function
debounce(
this.onElementResize.bind(this),
this.options.debounce
)(entries);
}
);
// Create a new ResizeObserver and pass in the function to be called when the element is resized
const debouncedOnElementResize = debounce(
this.onElementResize.bind(this),
this.options.debounce
);
this.resizeObserver = new ResizeObserver(
(entries: ResizeObserverEntry[]) => {
debouncedOnElementResize(entries);
}
);

Comment on lines +158 to +163
} else if (this.currentComponentWidth === 0) {
// eslint-disable-next-line no-console
console.warn(
`resize-controller: No width detected in <${this.host.localName}>. Please confirm it has display: block`
);
}
Copy link

Choose a reason for hiding this comment

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

Logging a warning to the console for a width of 0 may not always indicate an issue, especially for initially hidden elements.

Consider a more robust way to handle or document this scenario to avoid confusion.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between b769a0c and 29ff7ba.
Files selected for processing (1)
  • packages/controllers/resize-controller/README.md (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/controllers/resize-controller/README.md

@glitchgirl
Copy link
Contributor

this needs to be updated @shaal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ready for Review Type: Controller Suggestion Use this label for any issue that is a recommendation for a specific type of controller.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants