@tamagui/font-size 1.0.1-beta.59 → 1.0.1-beta.60

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tamagui/font-size",
3
- "version": "1.0.1-beta.59",
3
+ "version": "1.0.1-beta.60",
4
4
  "sideEffects": false,
5
5
  "source": "src/index.ts",
6
6
  "types": "./types/index.d.ts",
@@ -8,6 +8,7 @@
8
8
  "module": "dist/esm",
9
9
  "module:jsx": "dist/jsx",
10
10
  "files": [
11
+ "src",
11
12
  "types",
12
13
  "dist"
13
14
  ],
@@ -16,14 +17,14 @@
16
17
  "watch": "tamagui-build --watch"
17
18
  },
18
19
  "dependencies": {
19
- "@tamagui/core": "^1.0.1-beta.59"
20
+ "@tamagui/core": "^1.0.1-beta.60"
20
21
  },
21
22
  "peerDependencies": {
22
23
  "react": "*",
23
24
  "react-dom": "*"
24
25
  },
25
26
  "devDependencies": {
26
- "@tamagui/build": "^1.0.1-beta.59",
27
+ "@tamagui/build": "^1.0.1-beta.60",
27
28
  "react": "*",
28
29
  "react-dom": "*"
29
30
  },
@@ -0,0 +1,56 @@
1
+ import { FontSizeTokens, FontTokens, getConfig, getTokens, isVariable } from '@tamagui/core'
2
+
3
+ type GetFontSizeOpts = {
4
+ relativeSize?: number
5
+ font?: FontTokens
6
+ }
7
+
8
+ export const getFontSize = (
9
+ inSize: FontSizeTokens | null | undefined,
10
+ opts?: GetFontSizeOpts
11
+ ): number => {
12
+ const res = getFontSizeVariable(inSize, opts)
13
+ if (isVariable(res)) {
14
+ return +res.val
15
+ }
16
+ return res ? +res : 16
17
+ }
18
+
19
+ export const getFontSizeVariable = (
20
+ inSize: FontSizeTokens | null | undefined,
21
+ opts?: GetFontSizeOpts
22
+ ) => {
23
+ const token = getFontSizeToken(inSize, opts)
24
+ if (!token) {
25
+ return inSize
26
+ }
27
+ const conf = getConfig()
28
+ return conf.fontsParsed[opts?.font || '$body'].size[token]
29
+ }
30
+
31
+ export const getFontSizeToken = (
32
+ inSize: FontSizeTokens | null | undefined,
33
+ opts?: GetFontSizeOpts
34
+ ): FontSizeTokens | null => {
35
+ if (typeof inSize === 'number') {
36
+ return null
37
+ }
38
+ const size = inSize || '$4'
39
+ const relativeSize = opts?.relativeSize || 0
40
+ const conf = getConfig()
41
+ const fontSize = conf.fontsParsed[opts?.font || '$body'].size
42
+ const sizeTokens = Object.keys(fontSize)
43
+ let foundIndex = sizeTokens.indexOf(size)
44
+ if (foundIndex === -1) {
45
+ if (size.endsWith('.5')) {
46
+ foundIndex = sizeTokens.indexOf(size.replace('.5', ''))
47
+ }
48
+ }
49
+ if (process.env.NODE_ENV === 'development') {
50
+ if (foundIndex === -1) {
51
+ console.warn('No font size found', inSize, opts, 'in size tokens', sizeTokens)
52
+ }
53
+ }
54
+ const tokenIndex = Math.min(Math.max(0, foundIndex + relativeSize), sizeTokens.length - 1)
55
+ return (sizeTokens[tokenIndex] ?? size) as FontSizeTokens
56
+ }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './getFontSize'