Skip to content

Commit

Permalink
fix: Fix background color and text positioning issues (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatiPl01 authored Nov 4, 2023
1 parent 7b9168e commit dc4bacf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ yarn-error.log

dist/
react-native-skia-responsive-text*.tgz
.expo
26 changes: 18 additions & 8 deletions src/components/ResponsiveText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
AnimationSettings,
EllipsizeMode,
HorizontalAlignment,
PartialBy,
TextLineData,
TextOverflow,
VerticalAlignment
Expand All @@ -29,7 +28,7 @@ import TextLine from './TextLine';

const LINE_HEIGHT_MULTIPLIER = 1.5;

type ResponsiveTextProps = PartialBy<TextProps, 'x' | 'y'> & {
type ResponsiveTextProps = Omit<TextProps, 'x' | 'y'> & {
ellipsizeMode?: EllipsizeMode;
font: SkFont;
height?: number;
Expand All @@ -42,6 +41,8 @@ type ResponsiveTextProps = PartialBy<TextProps, 'x' | 'y'> & {
horizontalAlignment?: HorizontalAlignment;
lineHeight?: number;
verticalAlignment?: VerticalAlignment;
x?: number;
y?: number;
}> &
(
| { animationProgress?: SharedValue<number> }
Expand All @@ -55,7 +56,7 @@ type ResponsiveTextPrivateProps = ResponsiveTextProps & {

function ResponsiveText({
animationSettings: animationSettingsProp,
backgroundColor: backgroundColorProp = 'transparent',
backgroundColor: backgroundColorProp,
children,
ellipsizeMode,
font,
Expand All @@ -68,8 +69,8 @@ function ResponsiveText({
text = '',
verticalAlignment: verticalAlignmentProp = 'top',
width: widthProp,
x = 0,
y = 0,
x: xProp = 0,
y: yProp = 0,
...rest
}: ResponsiveTextPrivateProps) {
const fontSize = font.getSize();
Expand All @@ -86,7 +87,11 @@ function ResponsiveText({
}, [animationSettingsProp]);

// Create shared values from animatable props
const backgroundColor = useAnimatableValue(backgroundColorProp);
const x = useAnimatableValue(xProp);
const y = useAnimatableValue(yProp);
const backgroundColor = useAnimatableValue(
backgroundColorProp ?? 'transparent'
);
const lineHeight = useAnimatableValue(
lineHeightProp ?? LINE_HEIGHT_MULTIPLIER * fontSize
);
Expand Down Expand Up @@ -140,7 +145,7 @@ function ResponsiveText({
)
);

const backgroundComponent = backgroundColor && (
const backgroundComponent = backgroundColorProp && (
<Rect
color={backgroundColor}
height={backgroundHeight}
Expand Down Expand Up @@ -174,8 +179,13 @@ function ResponsiveText({
[backgroundHeight, width]
);

const groupTransform = useDerivedValue(() => [
{ translateX: x.value },
{ translateY: y.value }
]);

return (
<Group transform={[{ translateX: x }, { translateY: y }]}>
<Group transform={groupTransform}>
{overflow === 'hidden' ? (
<>
{backgroundComponent && (
Expand Down

0 comments on commit dc4bacf

Please sign in to comment.