@tamagui/text 1.0.0-beta.9 → 1.0.1-beta.102

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.
Files changed (56) hide show
  1. package/dist/cjs/Headings.js +79 -0
  2. package/dist/cjs/Headings.js.map +7 -0
  3. package/dist/cjs/SizableText.js +3 -1
  4. package/dist/cjs/SizableText.js.map +2 -2
  5. package/dist/cjs/index.js +2 -0
  6. package/dist/cjs/index.js.map +2 -2
  7. package/dist/cjs/wrapChildrenInText.js +108 -0
  8. package/dist/cjs/wrapChildrenInText.js.map +7 -0
  9. package/dist/cjs/wrapChildrenInText.test.js +47 -0
  10. package/dist/cjs/wrapChildrenInText.test.js.map +7 -0
  11. package/dist/esm/Headings.js +50 -0
  12. package/dist/esm/Headings.js.map +7 -0
  13. package/dist/esm/SizableText.js +4 -2
  14. package/dist/esm/SizableText.js.map +2 -2
  15. package/dist/esm/index.js +2 -0
  16. package/dist/esm/index.js.map +2 -2
  17. package/dist/esm/wrapChildrenInText.js +84 -0
  18. package/dist/esm/wrapChildrenInText.js.map +7 -0
  19. package/dist/esm/wrapChildrenInText.test.js +32 -0
  20. package/dist/esm/wrapChildrenInText.test.js.map +7 -0
  21. package/dist/jsx/Headings.js +50 -0
  22. package/dist/jsx/Headings.js.map +7 -0
  23. package/dist/jsx/Paragraph.js +1 -0
  24. package/dist/jsx/Paragraph.js.map +7 -0
  25. package/dist/jsx/SizableText.js +5 -2
  26. package/dist/jsx/SizableText.js.map +7 -0
  27. package/dist/jsx/Texts.js +1 -0
  28. package/dist/jsx/Texts.js.map +7 -0
  29. package/dist/jsx/index.js +3 -0
  30. package/dist/jsx/index.js.map +7 -0
  31. package/dist/jsx/wrapChildrenInText.js +61 -0
  32. package/dist/jsx/wrapChildrenInText.js.map +7 -0
  33. package/dist/jsx/wrapChildrenInText.test.js +29 -0
  34. package/dist/jsx/wrapChildrenInText.test.js.map +7 -0
  35. package/package.json +15 -7
  36. package/src/Headings.tsx +48 -0
  37. package/src/Paragraph.tsx +12 -0
  38. package/src/SizableText.tsx +22 -0
  39. package/src/Texts.tsx +0 -0
  40. package/src/index.tsx +4 -0
  41. package/src/wrapChildrenInText.test.tsx +40 -0
  42. package/src/wrapChildrenInText.tsx +88 -0
  43. package/types/Headings.d.ts +1210 -0
  44. package/types/Headings.d.ts.map +1 -0
  45. package/types/Paragraph.d.ts +0 -0
  46. package/types/Paragraph.d.ts.map +0 -0
  47. package/types/SizableText.d.ts +0 -0
  48. package/types/SizableText.d.ts.map +1 -1
  49. package/types/Texts.d.ts +0 -0
  50. package/types/Texts.d.ts.map +0 -0
  51. package/types/index.d.ts +2 -0
  52. package/types/index.d.ts.map +1 -1
  53. package/types/wrapChildrenInText.d.ts +20 -0
  54. package/types/wrapChildrenInText.d.ts.map +1 -0
  55. package/types/wrapChildrenInText.test.d.ts +2 -0
  56. package/types/wrapChildrenInText.test.d.ts.map +1 -0
@@ -1,14 +1,17 @@
1
- import { Text, getTextSize, styled } from "@tamagui/core";
1
+ import { Text, getFont, styled } from "@tamagui/core";
2
2
  const SizableText = styled(Text, {
3
3
  name: "SizableText",
4
4
  fontFamily: "$body",
5
5
  variants: {
6
- size: getTextSize
6
+ size: getFont
7
7
  },
8
8
  defaultVariants: {
9
9
  size: "$4"
10
10
  }
11
+ }, {
12
+ inlineWhenUnflattened: /* @__PURE__ */ new Set(["fontFamily"])
11
13
  });
12
14
  export {
13
15
  SizableText
14
16
  };
17
+ //# sourceMappingURL=SizableText.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/SizableText.tsx"],
4
+ "sourcesContent": ["import { GetProps, Text, getFont, styled } from '@tamagui/core'\n\nexport const SizableText = styled(\n Text,\n {\n name: 'SizableText',\n fontFamily: '$body',\n\n variants: {\n size: getFont,\n },\n\n defaultVariants: {\n size: '$4',\n },\n },\n {\n inlineWhenUnflattened: new Set(['fontFamily']),\n }\n)\n\nexport type SizableTextProps = GetProps<typeof SizableText>\n"],
5
+ "mappings": "AAAA;AAEO,MAAM,cAAc,OACzB,MACA;AAAA,EACE,MAAM;AAAA,EACN,YAAY;AAAA,EAEZ,UAAU;AAAA,IACR,MAAM;AAAA,EACR;AAAA,EAEA,iBAAiB;AAAA,IACf,MAAM;AAAA,EACR;AACF,GACA;AAAA,EACE,uBAAuB,oBAAI,IAAI,CAAC,YAAY,CAAC;AAC/C,CACF;",
6
+ "names": []
7
+ }
package/dist/jsx/Texts.js CHANGED
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=Texts.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [],
4
+ "sourcesContent": [],
5
+ "mappings": "",
6
+ "names": []
7
+ }
package/dist/jsx/index.js CHANGED
@@ -1,2 +1,5 @@
1
1
  export * from "./SizableText";
2
2
  export * from "./Paragraph";
3
+ export * from "./Headings";
4
+ export * from "./wrapChildrenInText";
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/index.tsx"],
4
+ "sourcesContent": ["export * from './SizableText'\nexport * from './Paragraph'\nexport * from './Headings'\nexport * from './wrapChildrenInText'\n"],
5
+ "mappings": "AAAA;AACA;AACA;AACA;",
6
+ "names": []
7
+ }
@@ -0,0 +1,61 @@
1
+ import React from "react";
2
+ function wrapChildrenInText(TextComponent, {
3
+ children,
4
+ textProps,
5
+ size,
6
+ noTextWrap,
7
+ color,
8
+ fontFamily,
9
+ fontSize,
10
+ fontWeight,
11
+ letterSpacing,
12
+ textAlign
13
+ }) {
14
+ if (noTextWrap || !children) {
15
+ return children;
16
+ }
17
+ let allChildren = React.Children.toArray(children);
18
+ let nextChildren = [];
19
+ let lastIsString = false;
20
+ const directTextProps = {};
21
+ if (color)
22
+ directTextProps.color = color;
23
+ if (fontFamily)
24
+ directTextProps.fontFamily = fontFamily;
25
+ if (fontSize)
26
+ directTextProps.fontSize = fontSize;
27
+ if (fontWeight)
28
+ directTextProps.fontWeight = fontWeight;
29
+ if (letterSpacing)
30
+ directTextProps.letterSpacing = letterSpacing;
31
+ if (textAlign)
32
+ directTextProps.textAlign = textAlign;
33
+ function concatStringChildren() {
34
+ if (!lastIsString)
35
+ return;
36
+ const index = nextChildren.length - 1;
37
+ const childrenStrings = nextChildren[index];
38
+ nextChildren[index] = <TextComponent key={index} {...directTextProps} size={size} {...textProps}>{childrenStrings}</TextComponent>;
39
+ }
40
+ for (const child of allChildren) {
41
+ const last = nextChildren[nextChildren.length - 1];
42
+ const isString = typeof child === "string";
43
+ if (isString) {
44
+ if (lastIsString) {
45
+ last.push(child);
46
+ } else {
47
+ nextChildren.push([child]);
48
+ }
49
+ } else {
50
+ concatStringChildren();
51
+ nextChildren.push(child);
52
+ }
53
+ lastIsString = isString;
54
+ }
55
+ concatStringChildren();
56
+ return nextChildren;
57
+ }
58
+ export {
59
+ wrapChildrenInText
60
+ };
61
+ //# sourceMappingURL=wrapChildrenInText.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/wrapChildrenInText.tsx"],
4
+ "sourcesContent": ["import type { SizeTokens } from '@tamagui/core'\nimport React from 'react'\n\nimport { SizableTextProps } from './SizableText'\n\nexport type TextParentStyles = {\n color?: SizableTextProps['color']\n fontWeight?: SizableTextProps['fontWeight']\n fontSize?: SizableTextProps['fontSize']\n fontFamily?: SizableTextProps['fontFamily']\n letterSpacing?: SizableTextProps['letterSpacing']\n textAlign?: SizableTextProps['textAlign']\n // all the other text controls\n textProps?: Partial<SizableTextProps>\n noTextWrap?: boolean\n}\n\ntype Props = TextParentStyles & {\n children?: React.ReactNode\n size?: SizeTokens\n}\n\nexport function wrapChildrenInText(\n TextComponent: any,\n {\n children,\n textProps,\n size,\n noTextWrap,\n color,\n fontFamily,\n fontSize,\n fontWeight,\n letterSpacing,\n textAlign,\n }: Props\n) {\n if (noTextWrap || !children) {\n return children\n }\n\n // in the case of using variables, like so:\n // <ListItem>Hello, {name}</ListItem>\n // it gives us props.children as ['Hello, ', 'name']\n // but we don't want to wrap multiple SizableText around each part\n // so we group them\n let allChildren = React.Children.toArray(children)\n let nextChildren: any[] = []\n let lastIsString = false\n const directTextProps: any = {}\n // to avoid setting undefined\n if (color) directTextProps.color = color\n if (fontFamily) directTextProps.fontFamily = fontFamily\n if (fontSize) directTextProps.fontSize = fontSize\n if (fontWeight) directTextProps.fontWeight = fontWeight\n if (letterSpacing) directTextProps.letterSpacing = letterSpacing\n if (textAlign) directTextProps.textAlign = textAlign\n\n function concatStringChildren() {\n if (!lastIsString) return\n const index = nextChildren.length - 1\n const childrenStrings = nextChildren[index]\n nextChildren[index] = (\n <TextComponent key={index} {...directTextProps} size={size} {...textProps}>\n {childrenStrings}\n </TextComponent>\n )\n }\n\n for (const child of allChildren) {\n const last = nextChildren[nextChildren.length - 1]\n const isString = typeof child === 'string'\n if (isString) {\n if (lastIsString) {\n last.push(child)\n } else {\n nextChildren.push([child])\n }\n } else {\n concatStringChildren()\n nextChildren.push(child)\n }\n lastIsString = isString\n }\n concatStringChildren()\n\n return nextChildren\n}\n"],
5
+ "mappings": "AACA;AAqBO,4BACL,eACA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,GAEF;AACA,MAAI,cAAc,CAAC,UAAU;AAC3B,WAAO;AAAA,EACT;AAOA,MAAI,cAAc,MAAM,SAAS,QAAQ,QAAQ;AACjD,MAAI,eAAsB,CAAC;AAC3B,MAAI,eAAe;AACnB,QAAM,kBAAuB,CAAC;AAE9B,MAAI;AAAO,oBAAgB,QAAQ;AACnC,MAAI;AAAY,oBAAgB,aAAa;AAC7C,MAAI;AAAU,oBAAgB,WAAW;AACzC,MAAI;AAAY,oBAAgB,aAAa;AAC7C,MAAI;AAAe,oBAAgB,gBAAgB;AACnD,MAAI;AAAW,oBAAgB,YAAY;AAE3C,kCAAgC;AAC9B,QAAI,CAAC;AAAc;AACnB,UAAM,QAAQ,aAAa,SAAS;AACpC,UAAM,kBAAkB,aAAa;AACrC,iBAAa,SACX,CAAC,cAAc,KAAK,WAAW,iBAAiB,MAAM,UAAU,YAC7D,gBACH,EAFC;AAAA,EAIL;AAEA,aAAW,SAAS,aAAa;AAC/B,UAAM,OAAO,aAAa,aAAa,SAAS;AAChD,UAAM,WAAW,OAAO,UAAU;AAClC,QAAI,UAAU;AACZ,UAAI,cAAc;AAChB,aAAK,KAAK,KAAK;AAAA,MACjB,OAAO;AACL,qBAAa,KAAK,CAAC,KAAK,CAAC;AAAA,MAC3B;AAAA,IACF,OAAO;AACL,2BAAqB;AACrB,mBAAa,KAAK,KAAK;AAAA,IACzB;AACA,mBAAe;AAAA,EACjB;AACA,uBAAqB;AAErB,SAAO;AACT;",
6
+ "names": []
7
+ }
@@ -0,0 +1,29 @@
1
+ import { expect, test } from "vitest";
2
+ import { wrapChildrenInText } from "./wrapChildrenInText";
3
+ const Comp = () => {
4
+ };
5
+ const re = <div data-id={Math.random()} />;
6
+ const reID = re.props["data-id"];
7
+ test("should wrap all string children, not wrap others", () => {
8
+ const out = wrapChildrenInText(Comp, {
9
+ children: [re, "a", "b", re, "c", re, "d"]
10
+ });
11
+ expect(out[0].props["data-id"]).toBe(reID);
12
+ expect(out[1].type).toBe(Comp);
13
+ expect(out[1].props.children).toStrictEqual(["a", "b"]);
14
+ expect(out[2].props["data-id"]).toBe(reID);
15
+ expect(out[3].type).toBe(Comp);
16
+ expect(out[3].props.children).toStrictEqual(["c"]);
17
+ expect(out[4].props["data-id"]).toBe(reID);
18
+ expect(out[5].type).toBe(Comp);
19
+ expect(out[5].props.children).toStrictEqual(["d"]);
20
+ });
21
+ test("should first child string", () => {
22
+ const out = wrapChildrenInText(Comp, {
23
+ children: ["a", re]
24
+ });
25
+ expect(out[0].type).toBe(Comp);
26
+ expect(out[0].props.children).toStrictEqual(["a"]);
27
+ expect(out[1].props["data-id"]).toBe(reID);
28
+ });
29
+ //# sourceMappingURL=wrapChildrenInText.test.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/wrapChildrenInText.test.tsx"],
4
+ "sourcesContent": ["import React from 'react'\nimport { expect, test } from 'vitest'\n\nimport { wrapChildrenInText } from './wrapChildrenInText'\n\nconst Comp = () => {}\nconst re = <div data-id={Math.random()} />\nconst reID = re.props['data-id']\n\ntest('should wrap all string children, not wrap others', () => {\n const out = wrapChildrenInText(Comp, {\n children: [re, 'a', 'b', re, 'c', re, 'd'],\n }) as any\n\n expect(out[0].props['data-id']).toBe(reID)\n\n expect(out[1].type).toBe(Comp)\n expect(out[1].props.children).toStrictEqual(['a', 'b'])\n\n expect(out[2].props['data-id']).toBe(reID)\n\n expect(out[3].type).toBe(Comp)\n expect(out[3].props.children).toStrictEqual(['c'])\n\n expect(out[4].props['data-id']).toBe(reID)\n\n expect(out[5].type).toBe(Comp)\n expect(out[5].props.children).toStrictEqual(['d'])\n})\n\ntest('should first child string', () => {\n const out = wrapChildrenInText(Comp, {\n children: ['a', re],\n }) as any\n\n expect(out[0].type).toBe(Comp)\n expect(out[0].props.children).toStrictEqual(['a'])\n\n expect(out[1].props['data-id']).toBe(reID)\n})\n"],
5
+ "mappings": "AACA;AAEA;AAEA,MAAM,OAAO,MAAM;AAAC;AACpB,MAAM,KAAK,CAAC,IAAI,SAAS,KAAK,OAAO,GAAG;AACxC,MAAM,OAAO,GAAG,MAAM;AAEtB,KAAK,oDAAoD,MAAM;AAC7D,QAAM,MAAM,mBAAmB,MAAM;AAAA,IACnC,UAAU,CAAC,IAAI,KAAK,KAAK,IAAI,KAAK,IAAI,GAAG;AAAA,EAC3C,CAAC;AAED,SAAO,IAAI,GAAG,MAAM,UAAU,EAAE,KAAK,IAAI;AAEzC,SAAO,IAAI,GAAG,IAAI,EAAE,KAAK,IAAI;AAC7B,SAAO,IAAI,GAAG,MAAM,QAAQ,EAAE,cAAc,CAAC,KAAK,GAAG,CAAC;AAEtD,SAAO,IAAI,GAAG,MAAM,UAAU,EAAE,KAAK,IAAI;AAEzC,SAAO,IAAI,GAAG,IAAI,EAAE,KAAK,IAAI;AAC7B,SAAO,IAAI,GAAG,MAAM,QAAQ,EAAE,cAAc,CAAC,GAAG,CAAC;AAEjD,SAAO,IAAI,GAAG,MAAM,UAAU,EAAE,KAAK,IAAI;AAEzC,SAAO,IAAI,GAAG,IAAI,EAAE,KAAK,IAAI;AAC7B,SAAO,IAAI,GAAG,MAAM,QAAQ,EAAE,cAAc,CAAC,GAAG,CAAC;AACnD,CAAC;AAED,KAAK,6BAA6B,MAAM;AACtC,QAAM,MAAM,mBAAmB,MAAM;AAAA,IACnC,UAAU,CAAC,KAAK,EAAE;AAAA,EACpB,CAAC;AAED,SAAO,IAAI,GAAG,IAAI,EAAE,KAAK,IAAI;AAC7B,SAAO,IAAI,GAAG,MAAM,QAAQ,EAAE,cAAc,CAAC,GAAG,CAAC;AAEjD,SAAO,IAAI,GAAG,MAAM,UAAU,EAAE,KAAK,IAAI;AAC3C,CAAC;",
6
+ "names": []
7
+ }
package/package.json CHANGED
@@ -1,31 +1,39 @@
1
1
  {
2
2
  "name": "@tamagui/text",
3
- "version": "1.0.0-beta.9",
4
- "sideEffects": false,
3
+ "version": "1.0.1-beta.102",
4
+ "sideEffects": [
5
+ "*.css"
6
+ ],
5
7
  "source": "src/index.ts",
6
- "typings": "types",
8
+ "types": "./types/index.d.ts",
7
9
  "main": "dist/cjs",
8
10
  "module": "dist/esm",
9
11
  "module:jsx": "dist/jsx",
10
12
  "files": [
13
+ "src",
11
14
  "types",
12
15
  "dist"
13
16
  ],
14
17
  "scripts": {
15
18
  "build": "tamagui-build",
16
- "watch": "tamagui-build --watch"
19
+ "watch": "tamagui-build --watch",
20
+ "clean": "tamagui-build clean",
21
+ "clean:build": "tamagui-build clean:build",
22
+ "test": "vitest --config ../vite-plugin-internal/vite.config.ts"
17
23
  },
18
24
  "dependencies": {
19
- "@tamagui/core": "^1.0.0-beta.9"
25
+ "@tamagui/core": "^1.0.1-beta.102"
20
26
  },
21
27
  "peerDependencies": {
22
28
  "react": "*",
23
29
  "react-dom": "*"
24
30
  },
25
31
  "devDependencies": {
26
- "@tamagui/build": "^1.0.0-beta.9",
32
+ "@tamagui/build": "^1.0.1-beta.102",
33
+ "@tamagui/vite-plugin-internal": "^1.0.1-beta.102",
27
34
  "react": "*",
28
- "react-dom": "*"
35
+ "react-dom": "*",
36
+ "vitest": "^0.17.1"
29
37
  },
30
38
  "publishConfig": {
31
39
  "access": "public"
@@ -0,0 +1,48 @@
1
+ import { styled } from '@tamagui/core'
2
+
3
+ import { Paragraph } from './Paragraph'
4
+
5
+ export const Heading = styled(Paragraph, {
6
+ tag: 'span',
7
+ name: 'Heading',
8
+ accessibilityRole: 'header',
9
+ fontFamily: '$heading',
10
+ size: '$8',
11
+ margin: 0,
12
+ })
13
+
14
+ export const H1 = styled(Heading, {
15
+ name: 'H1',
16
+ tag: 'h1',
17
+ size: '$10',
18
+ })
19
+
20
+ export const H2 = styled(Heading, {
21
+ name: 'H2',
22
+ tag: 'h2',
23
+ size: '$9',
24
+ })
25
+
26
+ export const H3 = styled(Heading, {
27
+ name: 'H3',
28
+ tag: 'h3',
29
+ size: '$8',
30
+ })
31
+
32
+ export const H4 = styled(Heading, {
33
+ name: 'H4',
34
+ tag: 'h4',
35
+ size: '$7',
36
+ })
37
+
38
+ export const H5 = styled(Heading, {
39
+ name: 'H5',
40
+ tag: 'h5',
41
+ size: '$6',
42
+ })
43
+
44
+ export const H6 = styled(Heading, {
45
+ name: 'H6',
46
+ tag: 'h6',
47
+ size: '$5',
48
+ })
@@ -0,0 +1,12 @@
1
+ import { PropTypes, styled } from '@tamagui/core'
2
+
3
+ import { SizableText } from './SizableText'
4
+
5
+ export const Paragraph = styled(SizableText, {
6
+ name: 'Paragraph',
7
+ tag: 'p',
8
+ selectable: true,
9
+ cursor: 'text',
10
+ })
11
+
12
+ export type ParagraphProps = PropTypes<typeof Paragraph>
@@ -0,0 +1,22 @@
1
+ import { GetProps, Text, getFont, styled } from '@tamagui/core'
2
+
3
+ export const SizableText = styled(
4
+ Text,
5
+ {
6
+ name: 'SizableText',
7
+ fontFamily: '$body',
8
+
9
+ variants: {
10
+ size: getFont,
11
+ },
12
+
13
+ defaultVariants: {
14
+ size: '$4',
15
+ },
16
+ },
17
+ {
18
+ inlineWhenUnflattened: new Set(['fontFamily']),
19
+ }
20
+ )
21
+
22
+ export type SizableTextProps = GetProps<typeof SizableText>
package/src/Texts.tsx ADDED
File without changes
package/src/index.tsx ADDED
@@ -0,0 +1,4 @@
1
+ export * from './SizableText'
2
+ export * from './Paragraph'
3
+ export * from './Headings'
4
+ export * from './wrapChildrenInText'
@@ -0,0 +1,40 @@
1
+ import React from 'react'
2
+ import { expect, test } from 'vitest'
3
+
4
+ import { wrapChildrenInText } from './wrapChildrenInText'
5
+
6
+ const Comp = () => {}
7
+ const re = <div data-id={Math.random()} />
8
+ const reID = re.props['data-id']
9
+
10
+ test('should wrap all string children, not wrap others', () => {
11
+ const out = wrapChildrenInText(Comp, {
12
+ children: [re, 'a', 'b', re, 'c', re, 'd'],
13
+ }) as any
14
+
15
+ expect(out[0].props['data-id']).toBe(reID)
16
+
17
+ expect(out[1].type).toBe(Comp)
18
+ expect(out[1].props.children).toStrictEqual(['a', 'b'])
19
+
20
+ expect(out[2].props['data-id']).toBe(reID)
21
+
22
+ expect(out[3].type).toBe(Comp)
23
+ expect(out[3].props.children).toStrictEqual(['c'])
24
+
25
+ expect(out[4].props['data-id']).toBe(reID)
26
+
27
+ expect(out[5].type).toBe(Comp)
28
+ expect(out[5].props.children).toStrictEqual(['d'])
29
+ })
30
+
31
+ test('should first child string', () => {
32
+ const out = wrapChildrenInText(Comp, {
33
+ children: ['a', re],
34
+ }) as any
35
+
36
+ expect(out[0].type).toBe(Comp)
37
+ expect(out[0].props.children).toStrictEqual(['a'])
38
+
39
+ expect(out[1].props['data-id']).toBe(reID)
40
+ })
@@ -0,0 +1,88 @@
1
+ import type { SizeTokens } from '@tamagui/core'
2
+ import React from 'react'
3
+
4
+ import { SizableTextProps } from './SizableText'
5
+
6
+ export type TextParentStyles = {
7
+ color?: SizableTextProps['color']
8
+ fontWeight?: SizableTextProps['fontWeight']
9
+ fontSize?: SizableTextProps['fontSize']
10
+ fontFamily?: SizableTextProps['fontFamily']
11
+ letterSpacing?: SizableTextProps['letterSpacing']
12
+ textAlign?: SizableTextProps['textAlign']
13
+ // all the other text controls
14
+ textProps?: Partial<SizableTextProps>
15
+ noTextWrap?: boolean
16
+ }
17
+
18
+ type Props = TextParentStyles & {
19
+ children?: React.ReactNode
20
+ size?: SizeTokens
21
+ }
22
+
23
+ export function wrapChildrenInText(
24
+ TextComponent: any,
25
+ {
26
+ children,
27
+ textProps,
28
+ size,
29
+ noTextWrap,
30
+ color,
31
+ fontFamily,
32
+ fontSize,
33
+ fontWeight,
34
+ letterSpacing,
35
+ textAlign,
36
+ }: Props
37
+ ) {
38
+ if (noTextWrap || !children) {
39
+ return children
40
+ }
41
+
42
+ // in the case of using variables, like so:
43
+ // <ListItem>Hello, {name}</ListItem>
44
+ // it gives us props.children as ['Hello, ', 'name']
45
+ // but we don't want to wrap multiple SizableText around each part
46
+ // so we group them
47
+ let allChildren = React.Children.toArray(children)
48
+ let nextChildren: any[] = []
49
+ let lastIsString = false
50
+ const directTextProps: any = {}
51
+ // to avoid setting undefined
52
+ if (color) directTextProps.color = color
53
+ if (fontFamily) directTextProps.fontFamily = fontFamily
54
+ if (fontSize) directTextProps.fontSize = fontSize
55
+ if (fontWeight) directTextProps.fontWeight = fontWeight
56
+ if (letterSpacing) directTextProps.letterSpacing = letterSpacing
57
+ if (textAlign) directTextProps.textAlign = textAlign
58
+
59
+ function concatStringChildren() {
60
+ if (!lastIsString) return
61
+ const index = nextChildren.length - 1
62
+ const childrenStrings = nextChildren[index]
63
+ nextChildren[index] = (
64
+ <TextComponent key={index} {...directTextProps} size={size} {...textProps}>
65
+ {childrenStrings}
66
+ </TextComponent>
67
+ )
68
+ }
69
+
70
+ for (const child of allChildren) {
71
+ const last = nextChildren[nextChildren.length - 1]
72
+ const isString = typeof child === 'string'
73
+ if (isString) {
74
+ if (lastIsString) {
75
+ last.push(child)
76
+ } else {
77
+ nextChildren.push([child])
78
+ }
79
+ } else {
80
+ concatStringChildren()
81
+ nextChildren.push(child)
82
+ }
83
+ lastIsString = isString
84
+ }
85
+ concatStringChildren()
86
+
87
+ return nextChildren
88
+ }