Skip to content

fix(UProgress): return correct max value on 0#5662

Closed
Shooteger wants to merge 3 commits into
nuxt:v4from
Shooteger:fix-uprogress-max-value-calc
Closed

fix(UProgress): return correct max value on 0#5662
Shooteger wants to merge 3 commits into
nuxt:v4from
Shooteger:fix-uprogress-max-value-calc

Conversation

@Shooteger
Copy link
Copy Markdown

🔗 Linked issue

resolves #5647

❓ Type of change

  • 📖 Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

This PR fixes division by zero when max prop is set to 0 by adding explicit check.
The problem was, when max={0}, the component performed (modelValue / 0) * 100, causing incorrect percentage calculation (returns 100 instead of 0).

I changed the code to if/else instead of a switch for readability and added an early return when maxValue === 0 to prevent division by zero and return the correct value.

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@github-actions github-actions Bot added the v4 #4488 label Dec 12, 2025
@Shooteger Shooteger changed the title fix: UProgress max value return fix(UProgress): return correct max value on 0 Dec 12, 2025
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Dec 12, 2025

npm i https://pkg.pr.new/@nuxt/ui@5662

commit: f12bc27

@Shooteger
Copy link
Copy Markdown
Author

@benjamincanac please review

@benjamincanac
Copy link
Copy Markdown
Member

The data-max still equals to 100 when max is set to 0 on the preview 🤔 https://ui-7xuv73uoq-nuxt-js.vercel.app/docs/components/progress#max

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 18, 2026

📝 Walkthrough

Walkthrough

The Progress.vue component's percent calculation logic is refactored from a switch-case structure to a guard-clause approach. The change introduces maxValue and currentModelValue variables to handle boundary conditions more explicitly. Specific checks are added for negative values, zero maximum values (which default to 100), and values exceeding the maximum. The percentage is then computed via direct division with rounding, with the final result clamped to [0, 100]. The refactoring addresses edge cases without altering the component's public API.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the bug being fixed: incorrect max value calculation when max equals 0, which directly reflects the main change in the PR.
Description check ✅ Passed The description explains the bug (division by zero when max=0), the root cause, and the solution (replacing switch with if/else and adding early return), all related to the changeset.
Linked Issues check ✅ Passed The PR directly addresses issue #5647 by preventing division by zero when max=0, implementing explicit boundary checks, and ensuring correct percentage calculation with proper handling of zero max values.
Out of Scope Changes check ✅ Passed All changes are focused on the percent calculation refactor in Progress.vue, directly addressing the division by zero bug without introducing unrelated modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
📝 Coding Plan
  • Generate coding plan for human review comments

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

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

CodeRabbit can approve the review once all CodeRabbit's comments are resolved.

Enable the reviews.request_changes_workflow setting to automatically approve the review once all CodeRabbit's comments are resolved.

Copy link
Copy Markdown
Contributor

@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: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/runtime/components/Progress.vue`:
- Around line 98-103: The percent calculation still treats max=0 as missing
because earlier code coerces props.max with a falsy check; change the falsy
check that uses "!props.max" to an explicit undefined/null check (e.g. props.max
=== undefined or props.max == null) so realMax/ maxValue will preserve 0; keep
the percent logic in the computed (maxValue = realMax.value ?? 100 and the
currentModelValue checks in the function that returns
Math.round((currentModelValue / maxValue) * 100)) so the new guard for maxValue
=== 0 will run and produce the correct 0% behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 057a3dd3-3678-4732-a0e0-12cd0cdf8460

📥 Commits

Reviewing files that changed from the base of the PR and between 2e55b3a and f12bc27.

📒 Files selected for processing (1)
  • src/runtime/components/Progress.vue

Comment on lines +98 to +103
const maxValue = realMax.value ?? 100
const currentModelValue = rootProps.value.modelValue!

if (currentModelValue < 0 || maxValue === 0) return 0
if (currentModelValue > maxValue) return 100
return Math.round((currentModelValue / maxValue) * 100)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

max === 0 is still coerced to default 100 due to upstream falsy check

The new guard on Line 101 won’t run for max={0} because Line 82 (!props.max) turns 0 into undefined, so Line 98 falls back to 100. This leaves the original bug (data-max=100 and wrong percent) unresolved.

💡 Proposed fix
 const realMax = computed(() => {
-  if (isIndeterminate.value || !props.max) {
+  if (isIndeterminate.value || props.max === null || props.max === undefined) {
     return undefined
   }

   if (Array.isArray(props.max)) {
     return props.max.length - 1
   }

   return Number(props.max)
 })

 const percent = computed(() => {
   if (isIndeterminate.value) {
     return undefined
   }

   const maxValue = realMax.value ?? 100
   const currentModelValue = rootProps.value.modelValue!

   if (currentModelValue < 0 || maxValue === 0) return 0
   if (currentModelValue > maxValue) return 100
   return Math.round((currentModelValue / maxValue) * 100)
 })
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/runtime/components/Progress.vue` around lines 98 - 103, The percent
calculation still treats max=0 as missing because earlier code coerces props.max
with a falsy check; change the falsy check that uses "!props.max" to an explicit
undefined/null check (e.g. props.max === undefined or props.max == null) so
realMax/ maxValue will preserve 0; keep the percent logic in the computed
(maxValue = realMax.value ?? 100 and the currentModelValue checks in the
function that returns Math.round((currentModelValue / maxValue) * 100)) so the
new guard for maxValue === 0 will run and produce the correct 0% behavior.

@benjamincanac
Copy link
Copy Markdown
Member

benjamincanac commented Mar 18, 2026

Closing this as Reka UI's ProgressRoot component doesn't support max={0}, it validates that max must be greater than 0 and defaults to 100 when it's not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v4 #4488

Projects

None yet

Development

Successfully merging this pull request may close these issues.

If UProgress max value = 0 -> UProgress max value = 100

2 participants