mirror of
https://github.com/zhigang1992/nativewind.git
synced 2026-06-15 10:17:54 +08:00
fix: improve active: styles on web
This commit is contained in:
@@ -6,6 +6,7 @@ import { withStyledChildren } from "./with-styled-children";
|
|||||||
import { withStyledProps } from "./with-styled-props";
|
import { withStyledProps } from "./with-styled-props";
|
||||||
import { useTailwind } from "./use-tailwind";
|
import { useTailwind } from "./use-tailwind";
|
||||||
import { withClassNames } from "./with-class-names";
|
import { withClassNames } from "./with-class-names";
|
||||||
|
import { usePlatform } from "./context/platform";
|
||||||
|
|
||||||
export interface StyledOptions<P> {
|
export interface StyledOptions<P> {
|
||||||
props?: Array<keyof P & string>;
|
props?: Array<keyof P & string>;
|
||||||
@@ -41,6 +42,8 @@ export function styled<T>(
|
|||||||
children: componentChildren,
|
children: componentChildren,
|
||||||
...componentProps
|
...componentProps
|
||||||
}: StyledProps<T>) {
|
}: StyledProps<T>) {
|
||||||
|
const { platform, preview } = usePlatform();
|
||||||
|
|
||||||
const { classes, allClasses, isComponent } = withClassNames({
|
const { classes, allClasses, isComponent } = withClassNames({
|
||||||
className,
|
className,
|
||||||
twClassName,
|
twClassName,
|
||||||
@@ -53,6 +56,8 @@ export function styled<T>(
|
|||||||
const { hover, focus, active, ...handlers } = useInteraction({
|
const { hover, focus, active, ...handlers } = useInteraction({
|
||||||
className: allClasses,
|
className: allClasses,
|
||||||
isComponent,
|
isComponent,
|
||||||
|
platform,
|
||||||
|
preview,
|
||||||
...componentProps,
|
...componentProps,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -70,6 +75,7 @@ export function styled<T>(
|
|||||||
componentProps,
|
componentProps,
|
||||||
spreadProps,
|
spreadProps,
|
||||||
cssProps,
|
cssProps,
|
||||||
|
preview,
|
||||||
});
|
});
|
||||||
|
|
||||||
const children = childStyles
|
const children = childStyles
|
||||||
|
|||||||
@@ -12,6 +12,15 @@ export interface Interaction extends PressableProps {
|
|||||||
active: boolean;
|
active: boolean;
|
||||||
hover: boolean;
|
hover: boolean;
|
||||||
focus: boolean;
|
focus: boolean;
|
||||||
|
onMouseDown?: PressableProps["onPressIn"];
|
||||||
|
onMouseUp?: PressableProps["onPressOut"];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UseInteractionOptions extends PressableProps {
|
||||||
|
className?: string;
|
||||||
|
isComponent?: boolean;
|
||||||
|
platform: string;
|
||||||
|
preview: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useInteraction({
|
export function useInteraction({
|
||||||
@@ -24,9 +33,10 @@ export function useInteraction({
|
|||||||
onHoverOut,
|
onHoverOut,
|
||||||
onPressIn,
|
onPressIn,
|
||||||
onPressOut,
|
onPressOut,
|
||||||
onPress,
|
|
||||||
className = "",
|
className = "",
|
||||||
}: PressableProps & { className?: string; isComponent?: boolean } = {}) {
|
preview,
|
||||||
|
platform,
|
||||||
|
}: UseInteractionOptions) {
|
||||||
const [hover, setHover] = useState(false);
|
const [hover, setHover] = useState(false);
|
||||||
const [focus, setFocus] = useState(false);
|
const [focus, setFocus] = useState(false);
|
||||||
const [active, setActive] = useState(false);
|
const [active, setActive] = useState(false);
|
||||||
@@ -123,19 +133,6 @@ export function useInteraction({
|
|||||||
[disabled, onPressOut, setActive]
|
[disabled, onPressOut, setActive]
|
||||||
);
|
);
|
||||||
|
|
||||||
const handlePress = useCallback(
|
|
||||||
(event: GestureResponderEvent) => {
|
|
||||||
if (disabled) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (onPress) {
|
|
||||||
onPress(event);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
[disabled, onPress, setActive]
|
|
||||||
);
|
|
||||||
|
|
||||||
const interaction: Interaction = {
|
const interaction: Interaction = {
|
||||||
active,
|
active,
|
||||||
hover,
|
hover,
|
||||||
@@ -148,7 +145,6 @@ export function useInteraction({
|
|||||||
onFocus: handleFocus,
|
onFocus: handleFocus,
|
||||||
onHoverIn: handleHoverIn,
|
onHoverIn: handleHoverIn,
|
||||||
onHoverOut: handleHoverOut,
|
onHoverOut: handleHoverOut,
|
||||||
onPress: handlePress,
|
|
||||||
onPressIn: handlePressIn,
|
onPressIn: handlePressIn,
|
||||||
onPressOut: handlePressOut,
|
onPressOut: handlePressOut,
|
||||||
});
|
});
|
||||||
@@ -163,9 +159,13 @@ export function useInteraction({
|
|||||||
interaction.onHoverOut = handleHoverOut;
|
interaction.onHoverOut = handleHoverOut;
|
||||||
}
|
}
|
||||||
if (className.includes("active:")) {
|
if (className.includes("active:")) {
|
||||||
interaction.onPress = handlePress;
|
|
||||||
interaction.onPressIn = handlePressIn;
|
interaction.onPressIn = handlePressIn;
|
||||||
interaction.onPressOut = handlePressOut;
|
interaction.onPressOut = handlePressOut;
|
||||||
|
|
||||||
|
if (platform === "web" && !preview) {
|
||||||
|
interaction.onMouseUp = handlePressOut;
|
||||||
|
interaction.onMouseDown = handlePressIn;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { StyleProp } from "react-native";
|
import { StyleProp } from "react-native";
|
||||||
import { usePlatform } from "./context/platform";
|
|
||||||
import { AtRuleRecord } from "./types/common";
|
import { AtRuleRecord } from "./types/common";
|
||||||
import { UseTailwindCallback } from "./use-tailwind";
|
import { UseTailwindCallback } from "./use-tailwind";
|
||||||
|
|
||||||
@@ -13,6 +12,7 @@ export interface WithStyledPropsOptions<S, T extends string> {
|
|||||||
tw: UseTailwindCallback<S>;
|
tw: UseTailwindCallback<S>;
|
||||||
spreadProps?: T[];
|
spreadProps?: T[];
|
||||||
cssProps?: T[];
|
cssProps?: T[];
|
||||||
|
preview: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type WithStyledProps<S, T extends string> = Record<T, unknown> & {
|
export type WithStyledProps<S, T extends string> = Record<T, unknown> & {
|
||||||
@@ -28,8 +28,8 @@ export function withStyledProps<S, T extends string>({
|
|||||||
componentProps,
|
componentProps,
|
||||||
spreadProps = [],
|
spreadProps = [],
|
||||||
cssProps = [],
|
cssProps = [],
|
||||||
|
preview,
|
||||||
}: WithStyledPropsOptions<S, T>): WithStyledProps<S, T> {
|
}: WithStyledPropsOptions<S, T>): WithStyledProps<S, T> {
|
||||||
const { preview } = usePlatform();
|
|
||||||
const mainStyles = tw(classes, { flatten: false });
|
const mainStyles = tw(classes, { flatten: false });
|
||||||
|
|
||||||
const styledProps: Partial<Record<T, unknown>> = {};
|
const styledProps: Partial<Record<T, unknown>> = {};
|
||||||
|
|||||||
Reference in New Issue
Block a user