Skip to content

feat(project): Defer creation of the component instance #10

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

Merged
merged 2 commits into from
Apr 4, 2025

Conversation

Hugoer
Copy link
Member

@Hugoer Hugoer commented Apr 3, 2025

This PR introduces an option to defer component instantiation until it becomes visible in the viewport.

Description

  • Added support for delayed instantiation based on viewport visibility.
  • Ensured full backward compatibility to avoid breaking existing implementations.
  • Provided an opt-in mechanism for projects that want to enable this behavior.

Related Issue

Fixes #9

Types of changes

  • 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 change)

Checklist:

  • My code follows the code style of this project.
  • I have read the CONTRIBUTING document.
  • All new and existing tests passed.

@Hugoer Hugoer requested a review from easingthemes April 3, 2025 15:36
Copy link
Member

@easingthemes easingthemes left a comment

Choose a reason for hiding this comment

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

Minor: nice to have, whatever you decide.

}, observerSettings);

observer.observe(element);
} else {
Copy link
Member

Choose a reason for hiding this comment

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

Minor: Maybe better to use early return instead of if/else, to follow same pattern as at the beginning of the function.

@@ -17,7 +17,7 @@ export const run = (element = window.document, initAttr = 'data-nc') =>
// get the component that needs, will load by attribute
Copy link
Member

Choose a reason for hiding this comment

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

Minor: Not sure if it's better to have observer here, when we have the node, instead in a factory.
Since in factory observer is called (created and destroyed) for each Component (data-nc="Com1, Com2, Com3"), while we need it only for element, regardless of Components.
This is not used that much, but still it's an option.

@easingthemes
Copy link
Member

Please run tests and update Checklist: in the description.

@easingthemes easingthemes merged commit 700605c into Netcentric:main Apr 4, 2025
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.

Defer component instantiation until it enters the viewport
2 participants