@onlynative/components 0.1.0-alpha.0 → 0.1.0-alpha.3
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/README.md +1 -1
- package/dist/appbar/index.js +37 -17
- package/dist/button/index.js +24 -13
- package/dist/checkbox/index.js +23 -12
- package/dist/chip/index.js +26 -14
- package/dist/icon-button/index.js +23 -12
- package/dist/index.js +52 -21
- package/dist/layout/index.d.ts +2 -2
- package/dist/switch/index.js +23 -12
- package/dist/text-field/index.js +24 -13
- package/dist/typography/index.d.ts +2 -2
- package/dist/typography/index.js +14 -5
- package/package.json +9 -3
- package/src/appbar/styles.ts +2 -2
- package/src/button/Button.tsx +4 -1
- package/src/button/styles.ts +5 -5
- package/src/card/styles.ts +4 -4
- package/src/checkbox/Checkbox.tsx +5 -1
- package/src/checkbox/styles.ts +4 -4
- package/src/chip/Chip.tsx +7 -1
- package/src/chip/styles.ts +4 -4
- package/src/icon-button/IconButton.tsx +2 -1
- package/src/icon-button/styles.ts +2 -2
- package/src/layout/resolveSpacing.ts +2 -2
- package/src/layout/types.ts +2 -2
- package/src/list/styles.ts +5 -5
- package/src/radio/styles.ts +5 -5
- package/src/switch/Switch.tsx +4 -1
- package/src/switch/styles.ts +4 -4
- package/src/test-utils/render-with-theme.tsx +2 -2
- package/src/text-field/TextField.tsx +4 -1
- package/src/text-field/styles.ts +3 -3
- package/src/typography/Typography.tsx +22 -10
- package/src/utils/icon.ts +30 -0
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { render, type RenderOptions } from '@testing-library/react-native'
|
|
2
2
|
import type { ReactElement } from 'react'
|
|
3
|
-
import {
|
|
3
|
+
import { ThemeProvider } from '@onlynative/core'
|
|
4
4
|
|
|
5
5
|
export function renderWithTheme(
|
|
6
6
|
ui: ReactElement,
|
|
7
7
|
options?: Omit<RenderOptions, 'wrapper'>,
|
|
8
8
|
) {
|
|
9
9
|
return render(ui, {
|
|
10
|
-
wrapper: ({ children }) => <
|
|
10
|
+
wrapper: ({ children }) => <ThemeProvider>{children}</ThemeProvider>,
|
|
11
11
|
...options,
|
|
12
12
|
})
|
|
13
13
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import MaterialCommunityIcons from '@expo/vector-icons/MaterialCommunityIcons'
|
|
2
1
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
|
3
2
|
import {
|
|
4
3
|
Animated,
|
|
@@ -11,6 +10,7 @@ import {
|
|
|
11
10
|
import type { NativeSyntheticEvent, TargetedEvent } from 'react-native'
|
|
12
11
|
import { useTheme } from '@onlynative/core'
|
|
13
12
|
|
|
13
|
+
import { getMaterialCommunityIcons } from '../utils/icon'
|
|
14
14
|
import { createStyles, labelPositions } from './styles'
|
|
15
15
|
import type { TextFieldProps } from './types'
|
|
16
16
|
|
|
@@ -46,6 +46,9 @@ export function TextField({
|
|
|
46
46
|
const isFilled = variant === 'filled'
|
|
47
47
|
const hasLeadingIcon = Boolean(leadingIcon)
|
|
48
48
|
|
|
49
|
+
const MaterialCommunityIcons =
|
|
50
|
+
leadingIcon || trailingIcon ? getMaterialCommunityIcons() : null
|
|
51
|
+
|
|
49
52
|
const { colors, styles } = useMemo(
|
|
50
53
|
() => createStyles(theme, variant),
|
|
51
54
|
[theme, variant],
|
package/src/text-field/styles.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { StyleSheet } from 'react-native'
|
|
2
|
-
import type {
|
|
2
|
+
import type { MaterialTheme } from '@onlynative/core'
|
|
3
3
|
|
|
4
4
|
import type { TextFieldVariant } from './types'
|
|
5
5
|
import { alphaColor } from '../utils/color'
|
|
@@ -51,7 +51,7 @@ interface VariantColors {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
function getVariantColors(
|
|
54
|
-
theme:
|
|
54
|
+
theme: MaterialTheme,
|
|
55
55
|
variant: TextFieldVariant,
|
|
56
56
|
): VariantColors {
|
|
57
57
|
const disabledOpacity = theme.stateLayer.disabledOpacity
|
|
@@ -92,7 +92,7 @@ function getVariantColors(
|
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
export function createStyles(theme:
|
|
95
|
+
export function createStyles(theme: MaterialTheme, variant: TextFieldVariant) {
|
|
96
96
|
const colors = getVariantColors(theme, variant)
|
|
97
97
|
const bodyLarge = theme.typography.bodyLarge
|
|
98
98
|
const bodySmall = theme.typography.bodySmall
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { useMemo } from 'react'
|
|
2
2
|
import type { ComponentType, ReactNode } from 'react'
|
|
3
3
|
import type { StyleProp, TextProps, TextStyle } from 'react-native'
|
|
4
|
-
import { Text } from 'react-native'
|
|
4
|
+
import { StyleSheet, Text } from 'react-native'
|
|
5
5
|
import { useTheme } from '@onlynative/core'
|
|
6
|
-
import type {
|
|
6
|
+
import type { MaterialTheme } from '@onlynative/core'
|
|
7
7
|
|
|
8
8
|
import type { TypographyVariant } from './types'
|
|
9
9
|
|
|
@@ -24,9 +24,9 @@ export interface TypographyProps extends Omit<TextProps, 'children' | 'style'> {
|
|
|
24
24
|
* @default 'bodyMedium'
|
|
25
25
|
*/
|
|
26
26
|
variant?: TypographyVariant
|
|
27
|
-
/** Override the text color. Defaults to the theme's `onSurface` color. */
|
|
27
|
+
/** Override the text color. Takes priority over `style.color`. Defaults to the theme's `onSurface` color. */
|
|
28
28
|
color?: string
|
|
29
|
-
/** Additional text styles
|
|
29
|
+
/** Additional text styles. Can override the default theme color via `style.color` when no `color` prop is set. */
|
|
30
30
|
style?: StyleProp<TextStyle>
|
|
31
31
|
/**
|
|
32
32
|
* Override the underlying text component (e.g. Animated.Text).
|
|
@@ -44,20 +44,32 @@ export function Typography({
|
|
|
44
44
|
accessibilityRole,
|
|
45
45
|
...textProps
|
|
46
46
|
}: TypographyProps) {
|
|
47
|
-
const theme = useTheme() as
|
|
47
|
+
const theme = useTheme() as MaterialTheme
|
|
48
48
|
const typographyStyle = theme.typography[variant]
|
|
49
|
-
const colorStyle = useMemo(
|
|
50
|
-
() => ({ color: color ?? theme.colors.onSurface }),
|
|
51
|
-
[color, theme.colors.onSurface],
|
|
52
|
-
)
|
|
53
49
|
const resolvedRole =
|
|
54
50
|
accessibilityRole ?? (HEADING_VARIANTS.has(variant) ? 'header' : undefined)
|
|
55
51
|
|
|
52
|
+
// When the consumer overrides fontSize via style, auto-adjust lineHeight
|
|
53
|
+
// proportionally so text isn't clipped inside overflow:hidden containers.
|
|
54
|
+
const lineHeightFix = useMemo(() => {
|
|
55
|
+
if (!style) return undefined
|
|
56
|
+
const flat = StyleSheet.flatten(style)
|
|
57
|
+
if (!flat?.fontSize || flat.lineHeight) return undefined
|
|
58
|
+
const ratio = typographyStyle.lineHeight / typographyStyle.fontSize
|
|
59
|
+
return { lineHeight: Math.ceil(flat.fontSize * ratio) }
|
|
60
|
+
}, [style, typographyStyle.fontSize, typographyStyle.lineHeight])
|
|
61
|
+
|
|
56
62
|
return (
|
|
57
63
|
<Component
|
|
58
64
|
{...textProps}
|
|
59
65
|
accessibilityRole={resolvedRole}
|
|
60
|
-
style={[
|
|
66
|
+
style={[
|
|
67
|
+
{ color: theme.colors.onSurface },
|
|
68
|
+
typographyStyle,
|
|
69
|
+
style,
|
|
70
|
+
lineHeightFix,
|
|
71
|
+
color != null ? { color } : undefined,
|
|
72
|
+
]}
|
|
61
73
|
>
|
|
62
74
|
{children}
|
|
63
75
|
</Component>
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2
|
+
let _MCIcons: any = null
|
|
3
|
+
let _resolved = false
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Lazily resolves MaterialCommunityIcons from `@expo/vector-icons`.
|
|
7
|
+
*
|
|
8
|
+
* Called at render time (not module load) so that components can be
|
|
9
|
+
* imported without `@expo/vector-icons` installed — the error only
|
|
10
|
+
* fires when an icon is actually rendered.
|
|
11
|
+
*/
|
|
12
|
+
export function getMaterialCommunityIcons() {
|
|
13
|
+
if (!_resolved) {
|
|
14
|
+
_resolved = true
|
|
15
|
+
try {
|
|
16
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
17
|
+
const mod = require('@expo/vector-icons/MaterialCommunityIcons')
|
|
18
|
+
_MCIcons = mod.default || mod
|
|
19
|
+
} catch {
|
|
20
|
+
_MCIcons = null
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
if (!_MCIcons) {
|
|
24
|
+
throw new Error(
|
|
25
|
+
'@expo/vector-icons is required for icon support. ' +
|
|
26
|
+
'Install it with: npx expo install @expo/vector-icons',
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
return _MCIcons
|
|
30
|
+
}
|