wordhug 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Cody Ta
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,88 @@
1
- # Temporary Holding Version
1
+ # wordhug
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Keep phrases together on one line in React. Put square brackets around a phrase and it is wrapped in an inline-block element, so it moves to the next line as one unit instead of breaking in the middle.
4
+
5
+ ```tsx
6
+ <WordHug>{"Hello [big world] today"}</WordHug>
7
+ // → Hello <span style="display: inline-block">big world</span> today
8
+ ```
9
+
10
+ Useful for headings and short copy where a line break would split a name, a price, or a phrase that should read as one unit.
11
+
12
+ ## Install
13
+
14
+ ```bash
15
+ npm install wordhug
16
+ ```
17
+
18
+ React 18 or later is a peer dependency.
19
+
20
+ ## Usage
21
+
22
+ ```tsx
23
+ import { WordHug } from "wordhug";
24
+
25
+ export function Hero() {
26
+ return (
27
+ <h1>
28
+ <WordHug>{"Fresh bread [delivered daily] to [your door]"}</WordHug>
29
+ </h1>
30
+ );
31
+ }
32
+ ```
33
+
34
+ `WordHug` has no hooks and no `"use client"` directive, so it renders in React Server Components as well as on the client.
35
+
36
+ ### Props
37
+
38
+ | Prop | Type | Default | Description |
39
+ | ----------- | ----------------------------- | ----------------------------- | ------------------------------------ |
40
+ | `children` | `string \| null \| undefined` | – | Text to render. Empty input renders nothing. |
41
+ | `as` | `React.ElementType` | `"span"` | Element that wraps each group. |
42
+ | `className` | `string` | – | Class name on each group wrapper. |
43
+ | `style` | `CSSProperties \| null` | `{ display: "inline-block" }` | Inline style on each group wrapper. Pass `null` to style groups only through `className`. |
44
+
45
+ Groups work without any stylesheet: each wrapper gets `display: inline-block` inline. To use a class instead (for example Tailwind's `inline-block` or `whitespace-nowrap`), pass `className` and `style={null}`.
46
+
47
+ An inline-block group moves to the next line as a whole. If a group is wider than its container, it still wraps inside its own box rather than overflowing. Use `style={{ whiteSpace: "nowrap" }}` if you would rather it never break and overflow instead. Note that a parent's `text-decoration` (such as a link underline) does not carry into inline-block groups.
48
+
49
+ ### Parsing rules
50
+
51
+ - `[...]` marks a group. Groups can nest: `[a [b c] d]`.
52
+ - A bracket without a partner is kept as literal text: `a ] b` and `a [ b` render unchanged.
53
+ - Runs of whitespace collapse to a single space, and whitespace at the start and end of the text and of each group is removed.
54
+ - Text touching a bracket stays attached: `(see [page 3])` renders `(see <span>page 3</span>)`.
55
+ - Empty groups (`[]`) are dropped.
56
+ - To show a literal bracket, put a backslash before it: `\[sic\]` renders `[sic]` and never forms a group. A backslash before any other character is kept as is.
57
+
58
+ In JSX text a backslash is literal, so `<WordHug>Quote \[sic\]</WordHug>` works as written. Inside a JavaScript string the backslash itself needs escaping: `{"Quote \\[sic\\]"}`.
59
+
60
+ ### `parseBrackets`
61
+
62
+ The parser is exported on its own and does not import React, so you can use the tree with any renderer.
63
+
64
+ ```ts
65
+ import { parseBrackets } from "wordhug";
66
+
67
+ parseBrackets("Hello [big world] today");
68
+ // [
69
+ // { type: "text", value: "Hello " },
70
+ // { type: "group", children: [{ type: "text", value: "big world" }] },
71
+ // { type: "text", value: " today" },
72
+ // ]
73
+ ```
74
+
75
+ Types `Node`, `TextNode`, `GroupNode` and `WordHugProps` are exported too.
76
+
77
+ ## Development
78
+
79
+ ```bash
80
+ npm install
81
+ npm test
82
+ npm run typecheck
83
+ npm run build
84
+ ```
85
+
86
+ ## License
87
+
88
+ MIT
package/dist/index.cjs ADDED
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ WordHug: () => WordHug,
24
+ parseBrackets: () => parseBrackets
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+
28
+ // src/parse.ts
29
+ var WHITESPACE = /[ \t\n\r\f\v]+/g;
30
+ function parseBrackets(input) {
31
+ const pairs = matchBrackets(input);
32
+ const stack = [{ nodes: [], end: -1 }];
33
+ let start = 0;
34
+ for (let i = 0; i < input.length; i++) {
35
+ const top = stack[stack.length - 1];
36
+ if (i === top.end) {
37
+ pushText(top.nodes, input.slice(start, i));
38
+ stack.pop();
39
+ stack[stack.length - 1].nodes.push({ type: "group", children: normalize(top.nodes) });
40
+ start = i + 1;
41
+ continue;
42
+ }
43
+ const close = pairs.get(i);
44
+ if (close !== void 0) {
45
+ pushText(top.nodes, input.slice(start, i));
46
+ stack.push({ nodes: [], end: close });
47
+ start = i + 1;
48
+ }
49
+ }
50
+ const root = stack[0].nodes;
51
+ pushText(root, input.slice(start));
52
+ return normalize(root);
53
+ }
54
+ var ESCAPED_BRACKET = /\\([[\]])/g;
55
+ function pushText(nodes, value) {
56
+ if (value) nodes.push({ type: "text", value: value.replace(ESCAPED_BRACKET, "$1") });
57
+ }
58
+ function matchBrackets(input) {
59
+ const pairs = /* @__PURE__ */ new Map();
60
+ const open = [];
61
+ for (let i = 0; i < input.length; i++) {
62
+ if (input[i] === "\\" && (input[i + 1] === "[" || input[i + 1] === "]")) i++;
63
+ else if (input[i] === "[") open.push(i);
64
+ else if (input[i] === "]" && open.length > 0) pairs.set(open.pop(), i);
65
+ }
66
+ return pairs;
67
+ }
68
+ function normalize(nodes) {
69
+ const out = [];
70
+ for (const node of nodes) {
71
+ if (node.type === "group") {
72
+ if (node.children.length > 0) out.push(node);
73
+ continue;
74
+ }
75
+ const last2 = out[out.length - 1];
76
+ if (last2?.type === "text") last2.value += node.value;
77
+ else out.push({ type: "text", value: node.value });
78
+ }
79
+ for (const node of out) {
80
+ if (node.type === "text") node.value = node.value.replace(WHITESPACE, " ");
81
+ }
82
+ const first = out[0];
83
+ if (first?.type === "text") first.value = first.value.trimStart();
84
+ const last = out[out.length - 1];
85
+ if (last?.type === "text") last.value = last.value.trimEnd();
86
+ return out.filter((node) => node.type === "group" || node.value !== "");
87
+ }
88
+
89
+ // src/word-hug.tsx
90
+ var import_jsx_runtime = require("react/jsx-runtime");
91
+ var DEFAULT_STYLE = { display: "inline-block" };
92
+ function WordHug({
93
+ children,
94
+ as: Tag = "span",
95
+ className,
96
+ style = DEFAULT_STYLE
97
+ }) {
98
+ if (!children) return null;
99
+ const renderNodes = (nodes) => nodes.map(
100
+ (node, i) => node.type === "text" ? node.value : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Tag, { className, style: style ?? void 0, children: renderNodes(node.children) }, i)
101
+ );
102
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: renderNodes(parseBrackets(children)) });
103
+ }
104
+ // Annotate the CommonJS export names for ESM import in node:
105
+ 0 && (module.exports = {
106
+ WordHug,
107
+ parseBrackets
108
+ });
@@ -0,0 +1,49 @@
1
+ import { ElementType, CSSProperties, ReactNode } from 'react';
2
+
3
+ interface TextNode {
4
+ type: "text";
5
+ value: string;
6
+ }
7
+ interface GroupNode {
8
+ type: "group";
9
+ children: Node[];
10
+ }
11
+ type Node = TextNode | GroupNode;
12
+ /**
13
+ * Parse text with square-bracket groups into a tree.
14
+ *
15
+ * - `[...]` becomes a group; groups can nest.
16
+ * - Brackets without a partner (`a ] b`, `a [ b`) are kept as literal text.
17
+ * - Runs of whitespace collapse to a single space, and whitespace at the start
18
+ * and end of the input and of each group is dropped.
19
+ * - Text directly touching a bracket stays attached: `(see [p. 3])` keeps the
20
+ * `(see ` and `)` around the group without extra spaces.
21
+ * - Empty groups (`[]`, `[ ]`) are removed.
22
+ * - `\[` and `\]` are literal brackets: they never open or close a group, and
23
+ * the backslash is removed. A backslash before anything else is kept as is.
24
+ */
25
+ declare function parseBrackets(input: string): Node[];
26
+
27
+ interface WordHugProps {
28
+ /** Text to render. `[...]` groups are kept together on one line. */
29
+ children: string | null | undefined;
30
+ /** Element used to wrap each group. Defaults to `"span"`. */
31
+ as?: ElementType;
32
+ /** Class name for each group wrapper. */
33
+ className?: string;
34
+ /**
35
+ * Inline style for each group wrapper. Defaults to `{ display: "inline-block" }`,
36
+ * so groups stay together without any stylesheet. Pass `null` to leave styling
37
+ * entirely to `className`.
38
+ */
39
+ style?: CSSProperties | null;
40
+ }
41
+ /**
42
+ * Render text so that every `[bracketed phrase]` is wrapped in an element
43
+ * (an inline-block span by default) that moves to the next line as one unit.
44
+ * Has no hooks or client-only code, so it works in React Server Components.
45
+ */
46
+ declare function WordHug({ children, as: Tag, className, style, }: WordHugProps): ReactNode;
47
+
48
+ export { WordHug, parseBrackets };
49
+ export type { GroupNode, Node, TextNode, WordHugProps };
@@ -0,0 +1,49 @@
1
+ import { ElementType, CSSProperties, ReactNode } from 'react';
2
+
3
+ interface TextNode {
4
+ type: "text";
5
+ value: string;
6
+ }
7
+ interface GroupNode {
8
+ type: "group";
9
+ children: Node[];
10
+ }
11
+ type Node = TextNode | GroupNode;
12
+ /**
13
+ * Parse text with square-bracket groups into a tree.
14
+ *
15
+ * - `[...]` becomes a group; groups can nest.
16
+ * - Brackets without a partner (`a ] b`, `a [ b`) are kept as literal text.
17
+ * - Runs of whitespace collapse to a single space, and whitespace at the start
18
+ * and end of the input and of each group is dropped.
19
+ * - Text directly touching a bracket stays attached: `(see [p. 3])` keeps the
20
+ * `(see ` and `)` around the group without extra spaces.
21
+ * - Empty groups (`[]`, `[ ]`) are removed.
22
+ * - `\[` and `\]` are literal brackets: they never open or close a group, and
23
+ * the backslash is removed. A backslash before anything else is kept as is.
24
+ */
25
+ declare function parseBrackets(input: string): Node[];
26
+
27
+ interface WordHugProps {
28
+ /** Text to render. `[...]` groups are kept together on one line. */
29
+ children: string | null | undefined;
30
+ /** Element used to wrap each group. Defaults to `"span"`. */
31
+ as?: ElementType;
32
+ /** Class name for each group wrapper. */
33
+ className?: string;
34
+ /**
35
+ * Inline style for each group wrapper. Defaults to `{ display: "inline-block" }`,
36
+ * so groups stay together without any stylesheet. Pass `null` to leave styling
37
+ * entirely to `className`.
38
+ */
39
+ style?: CSSProperties | null;
40
+ }
41
+ /**
42
+ * Render text so that every `[bracketed phrase]` is wrapped in an element
43
+ * (an inline-block span by default) that moves to the next line as one unit.
44
+ * Has no hooks or client-only code, so it works in React Server Components.
45
+ */
46
+ declare function WordHug({ children, as: Tag, className, style, }: WordHugProps): ReactNode;
47
+
48
+ export { WordHug, parseBrackets };
49
+ export type { GroupNode, Node, TextNode, WordHugProps };
package/dist/index.js ADDED
@@ -0,0 +1,80 @@
1
+ // src/parse.ts
2
+ var WHITESPACE = /[ \t\n\r\f\v]+/g;
3
+ function parseBrackets(input) {
4
+ const pairs = matchBrackets(input);
5
+ const stack = [{ nodes: [], end: -1 }];
6
+ let start = 0;
7
+ for (let i = 0; i < input.length; i++) {
8
+ const top = stack[stack.length - 1];
9
+ if (i === top.end) {
10
+ pushText(top.nodes, input.slice(start, i));
11
+ stack.pop();
12
+ stack[stack.length - 1].nodes.push({ type: "group", children: normalize(top.nodes) });
13
+ start = i + 1;
14
+ continue;
15
+ }
16
+ const close = pairs.get(i);
17
+ if (close !== void 0) {
18
+ pushText(top.nodes, input.slice(start, i));
19
+ stack.push({ nodes: [], end: close });
20
+ start = i + 1;
21
+ }
22
+ }
23
+ const root = stack[0].nodes;
24
+ pushText(root, input.slice(start));
25
+ return normalize(root);
26
+ }
27
+ var ESCAPED_BRACKET = /\\([[\]])/g;
28
+ function pushText(nodes, value) {
29
+ if (value) nodes.push({ type: "text", value: value.replace(ESCAPED_BRACKET, "$1") });
30
+ }
31
+ function matchBrackets(input) {
32
+ const pairs = /* @__PURE__ */ new Map();
33
+ const open = [];
34
+ for (let i = 0; i < input.length; i++) {
35
+ if (input[i] === "\\" && (input[i + 1] === "[" || input[i + 1] === "]")) i++;
36
+ else if (input[i] === "[") open.push(i);
37
+ else if (input[i] === "]" && open.length > 0) pairs.set(open.pop(), i);
38
+ }
39
+ return pairs;
40
+ }
41
+ function normalize(nodes) {
42
+ const out = [];
43
+ for (const node of nodes) {
44
+ if (node.type === "group") {
45
+ if (node.children.length > 0) out.push(node);
46
+ continue;
47
+ }
48
+ const last2 = out[out.length - 1];
49
+ if (last2?.type === "text") last2.value += node.value;
50
+ else out.push({ type: "text", value: node.value });
51
+ }
52
+ for (const node of out) {
53
+ if (node.type === "text") node.value = node.value.replace(WHITESPACE, " ");
54
+ }
55
+ const first = out[0];
56
+ if (first?.type === "text") first.value = first.value.trimStart();
57
+ const last = out[out.length - 1];
58
+ if (last?.type === "text") last.value = last.value.trimEnd();
59
+ return out.filter((node) => node.type === "group" || node.value !== "");
60
+ }
61
+
62
+ // src/word-hug.tsx
63
+ import { Fragment, jsx } from "react/jsx-runtime";
64
+ var DEFAULT_STYLE = { display: "inline-block" };
65
+ function WordHug({
66
+ children,
67
+ as: Tag = "span",
68
+ className,
69
+ style = DEFAULT_STYLE
70
+ }) {
71
+ if (!children) return null;
72
+ const renderNodes = (nodes) => nodes.map(
73
+ (node, i) => node.type === "text" ? node.value : /* @__PURE__ */ jsx(Tag, { className, style: style ?? void 0, children: renderNodes(node.children) }, i)
74
+ );
75
+ return /* @__PURE__ */ jsx(Fragment, { children: renderNodes(parseBrackets(children)) });
76
+ }
77
+ export {
78
+ WordHug,
79
+ parseBrackets
80
+ };
package/package.json CHANGED
@@ -1,6 +1,71 @@
1
1
  {
2
2
  "name": "wordhug",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Keep [bracketed phrases] together on one line in React by wrapping them in inline-block spans.",
5
+ "license": "MIT",
6
+ "author": "Cody Ta",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/codyta/wordhug.git"
10
+ },
11
+ "homepage": "https://github.com/codyta/wordhug#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/codyta/wordhug/issues"
14
+ },
15
+ "type": "module",
16
+ "sideEffects": false,
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "main": "./dist/index.cjs",
21
+ "module": "./dist/index.js",
22
+ "types": "./dist/index.d.ts",
23
+ "exports": {
24
+ ".": {
25
+ "import": {
26
+ "types": "./dist/index.d.ts",
27
+ "default": "./dist/index.js"
28
+ },
29
+ "require": {
30
+ "types": "./dist/index.d.cts",
31
+ "default": "./dist/index.cjs"
32
+ }
33
+ },
34
+ "./package.json": "./package.json"
35
+ },
36
+ "keywords": [
37
+ "react",
38
+ "text",
39
+ "line-break",
40
+ "no-wrap",
41
+ "inline-block",
42
+ "brackets"
43
+ ],
44
+ "scripts": {
45
+ "build": "tsup",
46
+ "test": "vitest run",
47
+ "test:watch": "vitest",
48
+ "typecheck": "tsc --noEmit",
49
+ "prepublishOnly": "npm run typecheck && npm test && npm run build",
50
+ "changeset": "changeset"
51
+ },
52
+ "peerDependencies": {
53
+ "react": ">=18"
54
+ },
55
+ "engines": {
56
+ "node": ">=18"
57
+ },
58
+ "devDependencies": {
59
+ "@changesets/cli": "^3.0.3",
60
+ "@testing-library/dom": "^10.4.2",
61
+ "@testing-library/react": "^16.3.3",
62
+ "@types/react": "^19.3.0",
63
+ "@types/react-dom": "^19.3.0",
64
+ "jsdom": "^31.0.1",
65
+ "react": "^19.3.1",
66
+ "react-dom": "^19.3.1",
67
+ "tsup": "^9.0.0",
68
+ "typescript": "^6.0.3",
69
+ "vitest": "^5.1.1"
70
+ }
71
+ }