Skip to content

feat(loading): [loading] added type definition and interface of Loading component to optimize loading style processing #3253

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 1 commit into from
Apr 7, 2025

Conversation

zzcr
Copy link
Member

@zzcr zzcr commented Apr 7, 2025

增加 Loading 组件的类型定义和接口,优化加载样式处理

PR

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message follows our Commit Message Guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Summary by CodeRabbit

  • New Features

    • Introduced mobile-first enhancements for placeholder components, now optimized for mobile display with dedicated mobile templates.
    • Improved loading overlay functionality with extended configuration options, offering a more customizable user experience.
  • Refactor

    • Streamlined integration with the Vue framework by refining shared type definitions, ensuring a smoother development and usage experience.

…ng component to optimize loading style processing
Copy link

coderabbitai bot commented Apr 7, 2025

Walkthrough

The changes update the component library to include mobile-first support for Skeleton components and expand the available type exports in Vue common adapters by adding the App type. Enhancements to the loading functionality introduce new TypeScript interfaces and update function signatures for improved type safety and error handling. The updates span modifications in component configuration, adapter type exports, and loading service implementations.

Changes

File(s) Change Summary
packages/modules.json Added "mobile-first" mode to Skeleton and SkeletonItem; introduced new components SkeletonItemMobileFirst and SkeletonMobileFirst with mobile-first templates.
packages/vue-common/src/adapter/{index.ts, vue2/index.ts, vue3/index.ts}
packages/vue-common/src/index.ts
Added export of the App type to extend available types for Vue adapters and public exports.
packages/vue/src/loading/index.ts Added new TypeScript interfaces: LoadingConfig, LoadingInstance, LoadingPlugin, and Root; updated the Loadings constant type to LoadingPlugin and replaced legacy TINYLoading implementation.
packages/vue/src/loading/src/service.ts Introduced TS interfaces for loading configuration and instance; updated default types for defaults and fullscreenLoading; refined function signatures (e.g., addStyle and default export) for type safety.

Sequence Diagram(s)

sequenceDiagram
  participant App
  participant Loadings as LoadingPlugin
  participant Service as LoadingService

  App->>Loadings: install(App)
  Loadings->>Service: Initialize loading with configs
  Service-->>Loadings: Return LoadingInstance
  Loadings->>App: Attach loading instance to app root
Loading

Suggested labels

enhancement

Poem

I'm a bunny in the code garden,
Hopping through changes, light and smart.
Mobile-first leaps and type-safe bounds,
Loadings flourish as I impart.
With whiskered cheer I celebrate,
A rabbit's dance in code's fine art.

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

 WARN  GET http://10.0.0.28:4873/@antfu%2Feslint-config error (503). Will retry in 10 seconds. 2 retries left.
 WARN  GET http://10.0.0.28:4873/@typescript-eslint%2Fparser error (503). Will retry in 10 seconds. 2 retries left.
 WARN  GET http://10.0.0.28:4873/eslint-config-prettier error (503). Will retry in 10 seconds. 2 retries left.
 WARN  GET http://10.0.0.28:4873/eslint-plugin-prettier error (503). Will retry in 10 seconds. 2 retries left.
 WARN  GET http://10.0.0.28:4873/eslint error (503). Will retry in 10 seconds. 2 retries left.
 WARN  GET http://10.0.0.28:4873/@types%2Feslint error (503). Will retry in 10 seconds. 2 retries left.
 WARN  GET http://10.0.0.28:4873/vue error (503). Will retry in 10 seconds. 2 retries left.
 WARN  GET http://10.0.0.28:4873/typescript error (503). Will retry in 10 seconds. 2 retries left.
 WARN  GET http://10.0.0.28:4873/@volar-plugins%2Feslint error (503). Will retry in 10 seconds. 2 retries left.
 WARN  GET http://10.0.0.28:4873/prettier error (503). Will retry in 10 seconds. 2 retries left.
 WARN  GET http://10.0.0.28:4873/@volar-plugins%2Fprettier error (503). Will retry in 10 seconds. 2 retries left.
 WARN  GET http://10.0.0.28:4873/fs-extra error (503). Will retry in 10 seconds. 2 retries left.
 WARN  GET http://10.0.0.28:4873/@vue%2Ftsconfig error (503). Will retry in 10 seconds. 2 retries left.
 WARN  GET http://10.0.0.28:4873/@antfu%2Feslint-config error (503). Will retry in 1 minute. 1 retries left.
 WARN  GET http://10.0.0.28:4873/eslint-plugin-prettier error (503). Will retry in 1 minute. 1 retries left.
 WARN  GET http://10.0.0.28:4873/@typescript-eslint%2Fparser error (503). Will retry in 1 minute. 1 retries left.
 WARN  GET http://10.0.0.28:4873/eslint-config-prettier error (503). Will retry in 1 minute. 1 retries left.
 WARN  GET http://10.0.0.28:4873/eslint error (503). Will retry in 1 minute. 1 retries left.
 WARN  GET http://10.0.0.28:4873/vue error (503). Will retry in 1 minute. 1 retries left.
 WARN  GET http://10.0.0.28:4873/@types%2Feslint error (503). Will retry in 1 minute. 1 retries left.
 WARN  GET http://10.0.0.28:4873/@volar-plugins%2Feslint error (503). Will retry in 1 minute. 1 retries left.
 WARN  GET http://10.0.0.28:4873/prettier error (503). Will retry in 1 minute. 1 retries left.
 WARN  GET http://10.0.0.28:4873/@volar-plugins%2Fprettier error (503). Will retry in 1 minute. 1 retries left.
 WARN  GET http://10.0.0.28:4873/fs-extra error (503). Will retry in 1 minute. 1 retries left.
 WARN  GET http://10.0.0.28:4873/typescript error (503). Will retry in 1 minute. 1 retries left.
 WARN  GET http://10.0.0.28:4873/@vue%2Ftsconfig error (503). Will retry in 1 minute. 1 retries left.
 ERR_PNPM_FETCH_503  GET http://10.0.0.28:4873/@antfu%2Feslint-config: Service Unavailable - 503

This error happened while installing a direct dependency of /tmp/eslint

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 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>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • 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 testing code 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added the enhancement New feature or request (功能增强) label Apr 7, 2025
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.

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1babf3a and 2266cc8.

📒 Files selected for processing (7)
  • packages/modules.json (2 hunks)
  • packages/vue-common/src/adapter/index.ts (1 hunks)
  • packages/vue-common/src/adapter/vue2/index.ts (1 hunks)
  • packages/vue-common/src/adapter/vue3/index.ts (1 hunks)
  • packages/vue-common/src/index.ts (1 hunks)
  • packages/vue/src/loading/index.ts (2 hunks)
  • packages/vue/src/loading/src/service.ts (4 hunks)
🔇 Additional comments (18)
packages/vue-common/src/adapter/index.ts (1)

11-12: Export of App type added correctly.

The addition of the App type to the exports list follows the established pattern and makes this essential Vue type available to consumers of the library. This aligns with the PR objective of enhancing type definitions for the Loading component.

packages/vue-common/src/adapter/vue3/index.ts (1)

496-497: App type export added for Vue 3 compatibility.

The addition of the App type to the Vue 3 adapter follows the same pattern as other type exports and maintains consistency with the changes in the other adapter files. This type is essential for components that need to interact with the Vue application instance.

packages/vue-common/src/index.ts (1)

380-381: App type properly re-exported from main entry point.

The addition of the App type to the main exports ensures it's available throughout the library. This change is consistent with the pattern established for other Vue types and properly exposes the type to external consumers.

packages/vue-common/src/adapter/vue2/index.ts (1)

356-357: App type export added for Vue 2 compatibility.

The addition of the App type from the Composition API package ensures compatibility for Vue 2 applications. This completes the consistent export of this type across all adapter interfaces, providing uniform type support regardless of the Vue version used.

packages/modules.json (4)

2240-2241: Enhancement to Skeleton component with mobile-first support.

Adding the "mobile-first" mode to the Skeleton component is a good improvement for responsive design, allowing the component to be optimized for mobile devices while maintaining PC support.


2249-2250: Enhancement to SkeletonItem component with mobile-first support.

Similar to the parent Skeleton component, adding mobile-first support to SkeletonItem ensures consistent responsive behavior across related components.


2253-2257: Addition of SkeletonItemMobileFirst template component.

The new component entry follows the established pattern in the codebase for mobile-first templates, providing the necessary template implementation for the mobile-first mode added to SkeletonItem.


2263-2267: Addition of SkeletonMobileFirst template component.

This completes the mobile-first implementation for the Skeleton component family, adding the corresponding template for the main Skeleton component's mobile-first mode.

packages/vue/src/loading/index.ts (7)

16-17: Added type imports for improved type safety.

Importing the App type from vue-common and ILoadingState from vue-renderless enhances type checking and provides better development experience.


19-31: Well-structured LoadingConfig interface.

The new LoadingConfig interface properly defines all configuration options for the loading component with appropriate types, improving type safety and documentation.


33-39: Clearly defined LoadingInstance interface.

This interface properly defines the structure of a loading instance, including its state, DOM element reference, and positioning properties.


41-56: Comprehensive type definitions for loading service.

The LoadingService type and LoadingPlugin interface definitions provide clear contracts for how the loading functionality should be used, improving developer experience and preventing errors.


58-65: Type-safe initialization of loading service.

The setupComponent.TINYLoading implementation with proper typing ensures the loading service is correctly attached to the root object. The ts-expect-error is properly documented to explain the runtime behavior.


67-69: Type-safe Loadings plugin definition.

Updating the Loadings constant to use the LoadingPlugin interface ensures type safety when it's used and installed.


79-80: Appropriate handling of window.Vue type uncertainty.

The @ts-expect-error comment properly acknowledges that window.Vue could be either Vue2 or Vue3, and that type compatibility will be handled at runtime.

packages/vue/src/loading/src/service.ts (3)

61-63: Type-safe function signature for addStyle.

Updating the addStyle function to use typed parameters improves type safety and code clarity.


69-71: Improved error handling with optional chaining.

The use of optional chaining (?.) when accessing properties on potentially undefined objects is a good practice to prevent runtime errors.

Also applies to: 79-81, 86-87


131-132:

❓ Verification inconclusive

Type assertion for createComponent result.

The type assertion for the createComponent result ensures type safety for the instance object. Consider adding runtime validation to ensure the created component matches the expected interface.


🏁 Script executed:

#!/bin/bash
# Check for usage of createComponent in the codebase
echo "Checking createComponent usage patterns in the codebase to ensure consistent typing..."
grep -r "createComponent" --include="*.ts" --include="*.js" packages/vue/src/ | head -n 20

Length of output: 1357


Action: Consider Optional Runtime Validation for Component Instances

The type assertion using as LoadingInstance provides static type safety, but it doesn't verify at runtime that the created component conforms to the expected interface. Our grep search across similar usages (e.g., in packages/vue/src/drawer and elsewhere) shows that runtime validation isn’t currently implemented anywhere. Please review whether adding runtime checks would enhance robustness in scenarios where the component might not fully meet the interface expectations.

  • File: packages/vue/src/loading/src/service.ts (line 131-132)
        }) as LoadingInstance
  • Suggestion: If runtime discrepancies are a concern, consider integrating runtime validation (possibly via a dedicated validation utility or library) to confirm the component’s structure conforms to the LoadingInstance interface.

Comment on lines +97 to 102
export default (configs: LoadingConfig = {}) => {
configs = { ...defaults, ...configs }

if (typeof configs.target === 'string') {
configs.target = document.querySelector(configs.target)
configs.target = document.querySelector(configs.target) as HTMLElement
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Unsafe type assertion for querySelector result.

The type assertion as HTMLElement on line 101 could be unsafe if the selector doesn't match any element, as it would assert null as HTMLElement.

Consider a more defensive approach:

-  configs.target = document.querySelector(configs.target) as HTMLElement
+  const targetElement = document.querySelector(configs.target)
+  if (!targetElement) {
+    console.warn(`[Loading] Target element not found: ${configs.target}`)
+  }
+  configs.target = targetElement as HTMLElement
📝 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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
export default (configs: LoadingConfig = {}) => {
configs = { ...defaults, ...configs }
if (typeof configs.target === 'string') {
configs.target = document.querySelector(configs.target)
configs.target = document.querySelector(configs.target) as HTMLElement
}
export default (configs: LoadingConfig = {}) => {
configs = { ...defaults, ...configs }
if (typeof configs.target === 'string') {
const targetElement = document.querySelector(configs.target)
if (!targetElement) {
console.warn(`[Loading] Target element not found: ${configs.target}`)
}
configs.target = targetElement as HTMLElement
}
// ... rest of the code
}

Comment on lines +17 to +37
import type { ILoadingState } from '@opentiny/vue-renderless/types/loading.type'

interface LoadingConfig {
text?: string | null
body?: boolean
lock?: boolean
customClass?: string
fullscreen?: boolean
iconSize?: string
target?: HTMLElement | string
size?: string
loadingImg?: string
tiny_mode?: string
}

interface LoadingInstance {
state: ILoadingState
$el: HTMLElement
originalPosition?: string
originalOverflow?: string
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Duplicate interface definitions between files.

The LoadingConfig and LoadingInstance interfaces are defined both here and in index.ts. Consider moving these to a shared types file to avoid duplication and potential inconsistencies.

// Create a new file: packages/vue/src/loading/types.ts
+export interface LoadingConfig {
+  text?: string | null
+  body?: boolean
+  lock?: boolean
+  customClass?: string
+  fullscreen?: boolean
+  iconSize?: string
+  target?: HTMLElement | string
+  size?: string
+  loadingImg?: string
+  tiny_mode?: string
+}
+
+export interface LoadingInstance {
+  state: ILoadingState
+  $el: HTMLElement
+  originalPosition?: string
+  originalOverflow?: string
+}

Then import these types in both files:

// In index.ts and service.ts
+import type { LoadingConfig, LoadingInstance } from './types'

Committable suggestion skipped: line range outside the PR's diff.

@kagol kagol merged commit 5a670fb into dev Apr 7, 2025
11 checks passed
@kagol kagol deleted the add-loading-types-0407 branch April 14, 2025 12:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request (功能增强)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants