Skip to content

[v10] Add character count countType: "words" option using Intl.Segmenter#1899

Merged
colinrotherham merged 13 commits into
support/10.xfrom
character-count-word-segmenter
Jun 1, 2026
Merged

[v10] Add character count countType: "words" option using Intl.Segmenter#1899
colinrotherham merged 13 commits into
support/10.xfrom
character-count-word-segmenter

Conversation

@colinrotherham

@colinrotherham colinrotherham commented Apr 22, 2026

Copy link
Copy Markdown
Contributor

Description

This PR extends #1895 to add character count support for countType: "words" using Intl.Segmenter

  {{ characterCount({
    label: {
      text: "Can you provide more detail?"
    },
    name: "more-detail",
-   maxwords: 200
+   maxlength: 200,
+   countType: "words"
  }) }}

Issues with separator regex

The existing word count behaviour uses /\S+/g to count all consecutive non-whitespace characters

But consider this phrase where hyphens and em-dashes are used as separators:

My mother-in-law—Wait, what?

It matches only 3 words currently:

["My", "mother-in-law—Wait,", "what?"]

Yet using the segmenter with granularity: "word" it matches 6 words:

["My", "mother", "in", "law", "Wait", "what"]

To solve this, but avoid breaking changes:

  • Setting countType: "words will use Intl.Segmenter in modern browsers
  • Setting maxwords (deprecated) will use the existing word count behaviour

But what about older browsers that lack Intl.Segmenter support?

They'll fall back to the static non-JS text customisable via textareaDescriptionText, for example:

You can enter up to 350 characters

Checklist

@colinrotherham colinrotherham temporarily deployed to nhsuk-frontend-pr-1899 April 22, 2026 13:37 Inactive
@colinrotherham colinrotherham added the Enhancement: feature request New feature or request label Apr 22, 2026
@colinrotherham colinrotherham changed the base branch from main to character-count-custom-function April 22, 2026 13:40
@colinrotherham colinrotherham force-pushed the character-count-word-segmenter branch from 4933601 to e51d1a4 Compare April 22, 2026 13:40
@colinrotherham colinrotherham temporarily deployed to nhsuk-frontend-pr-1899 April 22, 2026 13:41 Inactive
@colinrotherham colinrotherham force-pushed the character-count-custom-function branch from 6b80700 to 07676d2 Compare April 22, 2026 13:49
@colinrotherham colinrotherham force-pushed the character-count-word-segmenter branch from e51d1a4 to f61329c Compare April 22, 2026 13:50
@colinrotherham colinrotherham temporarily deployed to nhsuk-frontend-pr-1899 April 22, 2026 13:50 Inactive
@colinrotherham colinrotherham force-pushed the character-count-custom-function branch from 07676d2 to 649b198 Compare April 22, 2026 13:54
@colinrotherham colinrotherham force-pushed the character-count-word-segmenter branch from f61329c to 7fc007c Compare April 22, 2026 13:56
@colinrotherham colinrotherham temporarily deployed to nhsuk-frontend-pr-1899 April 22, 2026 13:56 Inactive
@colinrotherham colinrotherham temporarily deployed to nhsuk-frontend-pr-1899 April 22, 2026 14:05 Inactive
@colinrotherham colinrotherham force-pushed the character-count-custom-function branch from 649b198 to e76c2e7 Compare April 27, 2026 08:37
@colinrotherham colinrotherham force-pushed the character-count-word-segmenter branch from 8008c6a to 9e21bc3 Compare April 27, 2026 08:38
@colinrotherham colinrotherham temporarily deployed to nhsuk-frontend-pr-1899 April 27, 2026 08:38 Inactive
@colinrotherham colinrotherham force-pushed the character-count-custom-function branch 2 times, most recently from 6c80311 to 3290d27 Compare April 28, 2026 11:47
@colinrotherham colinrotherham force-pushed the character-count-word-segmenter branch from 9e21bc3 to ea4e0f4 Compare April 28, 2026 11:59
@colinrotherham colinrotherham temporarily deployed to nhsuk-frontend-pr-1899 April 28, 2026 11:59 Inactive
@colinrotherham colinrotherham force-pushed the character-count-custom-function branch from 3290d27 to 9492e12 Compare April 28, 2026 12:06
@colinrotherham colinrotherham force-pushed the character-count-word-segmenter branch from ea4e0f4 to 25511df Compare April 28, 2026 12:06
@colinrotherham colinrotherham temporarily deployed to nhsuk-frontend-pr-1899 April 28, 2026 12:06 Inactive
@colinrotherham colinrotherham force-pushed the character-count-custom-function branch from 9492e12 to e93b9da Compare April 28, 2026 14:15
@colinrotherham colinrotherham force-pushed the character-count-word-segmenter branch from 25511df to fa996f5 Compare April 28, 2026 14:17
@colinrotherham colinrotherham temporarily deployed to nhsuk-frontend-pr-1899 April 28, 2026 14:17 Inactive
@colinrotherham colinrotherham linked an issue Apr 28, 2026 that may be closed by this pull request
@colinrotherham colinrotherham force-pushed the character-count-word-segmenter branch from fa996f5 to 5c1cbb6 Compare April 28, 2026 17:02
@colinrotherham colinrotherham temporarily deployed to nhsuk-frontend-pr-1899 April 28, 2026 17:02 Inactive
@colinrotherham colinrotherham temporarily deployed to nhsuk-frontend-pr-1899 June 1, 2026 06:59 Inactive
@colinrotherham colinrotherham force-pushed the character-count-word-segmenter branch from 1becd9a to a6d1b7b Compare June 1, 2026 07:18
@colinrotherham colinrotherham temporarily deployed to nhsuk-frontend-pr-1899 June 1, 2026 07:18 Inactive
@colinrotherham

Copy link
Copy Markdown
Contributor Author

@anandamaryon1 I've added three changelog entries for:

  • Improved character count counting
  • Custom character count functions
  • Rename the character count maxwords option

If you can check please? 🙏

@colinrotherham colinrotherham force-pushed the character-count-word-segmenter branch from a6d1b7b to 00aafdb Compare June 1, 2026 09:36
@colinrotherham colinrotherham temporarily deployed to nhsuk-frontend-pr-1899 June 1, 2026 09:37 Inactive
@colinrotherham colinrotherham force-pushed the character-count-word-segmenter branch from 00aafdb to 614cba9 Compare June 1, 2026 09:53
@colinrotherham colinrotherham temporarily deployed to nhsuk-frontend-pr-1899 June 1, 2026 09:53 Inactive
@colinrotherham colinrotherham force-pushed the character-count-word-segmenter branch from 614cba9 to 9e0feef Compare June 1, 2026 10:39
@colinrotherham colinrotherham temporarily deployed to nhsuk-frontend-pr-1899 June 1, 2026 10:40 Inactive
@colinrotherham colinrotherham merged commit 0cea3dd into support/10.x Jun 1, 2026
23 checks passed
@colinrotherham colinrotherham deleted the character-count-word-segmenter branch June 1, 2026 10:59
@github-project-automation github-project-automation Bot moved this to Ready to release in Service Manual Sprint Board Jun 3, 2026
@colinrotherham colinrotherham moved this from Ready to release to 2026 - Done in Service Manual Sprint Board Jun 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: 2026 - Done

Development

Successfully merging this pull request may close these issues.

Character count component counts code points, not characters

3 participants