Skip to content

Commit

Permalink
Merge pull request #1769 from didi/feat-hover-style
Browse files Browse the repository at this point in the history
feat(view): view hover style
  • Loading branch information
hiyuki authored Jan 9, 2025
2 parents a7f09c0 + 157d6b8 commit e748b09
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const TAG_NAME = 'movable-view'

module.exports = function ({ print }) {
const aliEventLog = print({ platform: 'ali', tag: TAG_NAME, isError: false, type: 'event' })
const androidEventLog = print({ platform: 'android', tag: TAG_NAME, isError: false, type: 'event' })
const iosEventLog = print({ platform: 'ios', tag: TAG_NAME, isError: false, type: 'event' })
const qaPropLog = print({ platform: 'qa', tag: TAG_NAME, isError: false })
const androidPropLog = print({ platform: 'android', tag: TAG_NAME, isError: false })
const iosPropLog = print({ platform: 'ios', tag: TAG_NAME, isError: false })
Expand All @@ -27,7 +29,7 @@ module.exports = function ({ print }) {
android: androidPropLog
},
{
test: /^(inertia|damping|animation)$/,
test: /^(damping|friction|scale|scale-min|scale-max|scale-value)$/,
ios: iosPropLog,
android: androidPropLog
}
Expand All @@ -36,6 +38,11 @@ module.exports = function ({ print }) {
{
test: /^(htouchmove|vtouchmove)$/,
ali: aliEventLog
},
{
test: /^(bindscale)$/,
ios: iosEventLog,
android: androidEventLog
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module.exports = function ({ print }) {
qa: qaPropLog
},
{
test: /^(scroll-into-view|refresher-threshold|enable-passive|scroll-anchoring|using-sticky|fast-deceleration|enable-flex)$/,
test: /^(refresher-threshold|enable-passive|scroll-anchoring|using-sticky|fast-deceleration|enable-flex)$/,
android: androidPropLog,
ios: iosPropLog
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export interface IntersectionObserver {
}
}

export interface ScrollViewContextValue {
gestureRef: React.RefObject<any> | null
}

export const MovableAreaContext = createContext({ width: 0, height: 0 })

export const FormContext = createContext<FormContextValue | null>(null)
Expand All @@ -52,3 +56,5 @@ export const IntersectionObserverContext = createContext<IntersectionObserver |
export const RouteContext = createContext<number | null>(null)

export const KeyboardAvoidContext = createContext<KeyboardAvoidContextValue | null>(null)

export const ScrollViewContext = createContext<ScrollViewContextValue>({ gestureRef: null })
77 changes: 20 additions & 57 deletions packages/webpack-plugin/lib/runtime/components/react/mpx-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* ✘ bindagreeprivacyauthorization
* ✔ bindtap
*/
import { createElement, useEffect, useRef, useState, ReactNode, forwardRef, useContext, JSX } from 'react'
import { createElement, useEffect, useRef, ReactNode, forwardRef, useContext, JSX } from 'react'
import {
View,
StyleSheet,
Expand All @@ -45,10 +45,12 @@ import {
NativeSyntheticEvent
} from 'react-native'
import { warn } from '@mpxjs/utils'
import { getCurrentPage, splitProps, splitStyle, useLayout, useTransformStyle, wrapChildren, extendObject } from './utils'
import { GestureDetector, PanGesture } from 'react-native-gesture-handler'
import { getCurrentPage, splitProps, splitStyle, useLayout, useTransformStyle, wrapChildren, extendObject, useHover } from './utils'
import useInnerProps, { getCustomEvent } from './getInnerListeners'
import useNodesRef, { HandlerRef } from './useNodesRef'
import { RouteContext, FormContext } from './context'
import type { ExtendedViewStyle } from './types/common'

export type Type = 'default' | 'primary' | 'warn'

Expand All @@ -68,7 +70,7 @@ export interface ButtonProps {
disabled?: boolean
loading?: boolean
'hover-class'?: string
'hover-style'?: ViewStyle & TextStyle & Record<string, any>
'hover-style'?: ExtendedViewStyle
'hover-start-time'?: number
'hover-stay-time'?: number
'open-type'?: OpenType
Expand All @@ -83,8 +85,6 @@ export interface ButtonProps {
children: ReactNode
bindgetuserinfo?: (userInfo: any) => void
bindtap?: (evt: NativeSyntheticEvent<TouchEvent> | unknown) => void
bindtouchstart?: (evt: NativeSyntheticEvent<TouchEvent> | unknown) => void
bindtouchend?: (evt: NativeSyntheticEvent<TouchEvent> | unknown) => void
}

const LOADING_IMAGE_URI =
Expand Down Expand Up @@ -216,15 +216,16 @@ const Button = forwardRef<HandlerRef<View, ButtonProps>, ButtonProps>((buttonPro
style = {},
children,
bindgetuserinfo,
bindtap,
bindtouchstart,
bindtouchend
bindtap
} = props

const pageId = useContext(RouteContext)

const formContext = useContext(FormContext)

const enableHover = hoverClass !== 'none'
const { isHover, gesture } = useHover({ enableHover, hoverStartTime, hoverStayTime, disabled })

let submitFn: () => void | undefined
let resetFn: () => void | undefined

Expand All @@ -233,42 +234,30 @@ const Button = forwardRef<HandlerRef<View, ButtonProps>, ButtonProps>((buttonPro
resetFn = formContext.reset
}

const refs = useRef<{
hoverStartTimer: ReturnType<typeof setTimeout> | undefined
hoverStayTimer: ReturnType<typeof setTimeout> | undefined
}>({
hoverStartTimer: undefined,
hoverStayTimer: undefined
})

const [isHover, setIsHover] = useState(false)

const isMiniSize = size === 'mini'

const applyHoverEffect = isHover && hoverClass !== 'none'

const [color, hoverColor, plainColor, disabledColor] = TypeColorMap[type]

const normalBackgroundColor = disabled ? disabledColor : applyHoverEffect || loading ? hoverColor : color
const normalBackgroundColor = disabled ? disabledColor : isHover || loading ? hoverColor : color

const plainBorderColor = disabled
? 'rgba(0, 0, 0, .2)'
: applyHoverEffect
: isHover
? `rgba(${plainColor},.6)`
: `rgb(${plainColor})`

const normalBorderColor = type === 'default' ? 'rgba(0, 0, 0, .2)' : normalBackgroundColor

const plainTextColor = disabled
? 'rgba(0, 0, 0, .2)'
: applyHoverEffect
: isHover
? `rgba(${plainColor}, .6)`
: `rgb(${plainColor})`

const normalTextColor =
type === 'default'
? `rgba(0, 0, 0, ${disabled ? 0.3 : applyHoverEffect || loading ? 0.6 : 1})`
: `rgba(255 ,255 ,255 , ${disabled || applyHoverEffect || loading ? 0.6 : 1})`
? `rgba(0, 0, 0, ${disabled ? 0.3 : isHover || loading ? 0.6 : 1})`
: `rgba(255 ,255 ,255 , ${disabled || isHover || loading ? 0.6 : 1})`

const viewStyle = {
borderWidth: 1,
Expand Down Expand Up @@ -297,7 +286,7 @@ const Button = forwardRef<HandlerRef<View, ButtonProps>, ButtonProps>((buttonPro
{},
defaultStyle,
style,
applyHoverEffect ? hoverStyle : {}
isHover ? hoverStyle : {}
)

const {
Expand Down Expand Up @@ -366,34 +355,6 @@ const Button = forwardRef<HandlerRef<View, ButtonProps>, ButtonProps>((buttonPro
}
}

const setStayTimer = () => {
clearTimeout(refs.current.hoverStayTimer)
refs.current.hoverStayTimer = setTimeout(() => {
setIsHover(false)
clearTimeout(refs.current.hoverStayTimer)
}, hoverStayTime)
}

const setStartTimer = () => {
clearTimeout(refs.current.hoverStartTimer)
refs.current.hoverStartTimer = setTimeout(() => {
setIsHover(true)
clearTimeout(refs.current.hoverStartTimer)
}, hoverStartTime)
}

const onTouchStart = (evt: NativeSyntheticEvent<TouchEvent>) => {
bindtouchstart && bindtouchstart(evt)
if (disabled) return
setStartTimer()
}

const onTouchEnd = (evt: NativeSyntheticEvent<TouchEvent>) => {
bindtouchend && bindtouchend(evt)
if (disabled) return
setStayTimer()
}

const handleFormTypeFn = () => {
if (formType === 'submit') {
submitFn && submitFn()
Expand All @@ -418,8 +379,6 @@ const Button = forwardRef<HandlerRef<View, ButtonProps>, ButtonProps>((buttonPro
},
layoutProps,
{
bindtouchstart: (bindtouchstart || !disabled) && onTouchStart,
bindtouchend: (bindtouchend || !disabled) && onTouchEnd,
bindtap: !disabled && onTap
}
),
Expand All @@ -442,7 +401,7 @@ const Button = forwardRef<HandlerRef<View, ButtonProps>, ButtonProps>((buttonPro
}
)

return createElement(View, innerProps, loading && createElement(Loading, { alone: !children }),
const baseButton = createElement(View, innerProps, loading && createElement(Loading, { alone: !children }),
wrapChildren(
props,
{
Expand All @@ -453,6 +412,10 @@ const Button = forwardRef<HandlerRef<View, ButtonProps>, ButtonProps>((buttonPro
}
)
)

return enableHover
? createElement(GestureDetector, { gesture: gesture as PanGesture }, baseButton)
: baseButton
})

Button.displayName = 'MpxButton'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* ✘ scale-min
* ✘ scale-max
* ✘ scale-value
* animation
* animation
* ✔ bindchange
* ✘ bindscale
* ✔ htouchmove
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
* ✔ nodes
*/
import { View, ViewProps, ViewStyle } from 'react-native'
import { useRef, forwardRef, JSX, useState } from 'react'
import { useRef, forwardRef, JSX, useState, createElement } from 'react'
import useInnerProps from '../getInnerListeners'
import useNodesRef, { HandlerRef } from '../useNodesRef' // 引入辅助函数
import { useTransformStyle, useLayout } from '../utils'
import { useTransformStyle, useLayout, extendObject } from '../utils'
import { WebView, WebViewMessageEvent } from 'react-native-webview'
import { generateHTML } from './html'

Expand Down Expand Up @@ -91,28 +91,22 @@ const _RichText = forwardRef<HandlerRef<View, _RichTextProps>, _RichTextProps>((
layoutRef
})

const innerProps = useInnerProps(props, {
const innerProps = useInnerProps(props, extendObject({
ref: nodeRef,
style: { ...normalStyle, ...layoutStyle },
...layoutProps
}, [], {
style: extendObject(normalStyle, layoutStyle)
}, layoutProps), [], {
layoutRef
})

const html: string = typeof nodes === 'string' ? nodes : jsonToHtmlStr(nodes)

return (
<View
{...innerProps}
>
<WebView
source={{ html: generateHTML(html) }}
onMessage={(event: WebViewMessageEvent) => {
setWebViewHeight(+event.nativeEvent.data)
}}
>
</WebView>
</View>
return createElement(View, innerProps,
createElement(WebView, {
source: { html: generateHTML(html) },
onMessage: (event: WebViewMessageEvent) => {
setWebViewHeight(+event.nativeEvent.data)
}
})
)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
*/
import { ScrollView } from 'react-native-gesture-handler'
import { View, RefreshControl, NativeSyntheticEvent, NativeScrollEvent, LayoutChangeEvent, ViewStyle } from 'react-native'
import { JSX, ReactNode, RefObject, useRef, useState, useEffect, forwardRef, useContext, createElement } from 'react'
import { JSX, ReactNode, RefObject, useRef, useState, useEffect, forwardRef, useContext, createElement, useMemo } from 'react'
import { useAnimatedRef } from 'react-native-reanimated'
import { warn } from '@mpxjs/utils'
import useInnerProps, { getCustomEvent } from './getInnerListeners'
import useNodesRef, { HandlerRef } from './useNodesRef'
import { splitProps, splitStyle, useTransformStyle, useLayout, wrapChildren, extendObject, flatGesture, GestureHandler } from './utils'
import { IntersectionObserverContext } from './context'
import { IntersectionObserverContext, ScrollViewContext } from './context'

interface ScrollViewProps {
children?: ReactNode;
Expand Down Expand Up @@ -194,6 +194,12 @@ const _ScrollView = forwardRef<HandlerRef<ScrollView & View, ScrollViewProps>, S
gestureRef: scrollViewRef
})

const contextValue = useMemo(() => {
return {
gestureRef: scrollViewRef
}
}, [])

const { layoutRef, layoutStyle, layoutProps } = useLayout({ props, hasSelfPercent, setWidth, setHeight, nodeRef: scrollViewRef, onLayout })

if (scrollX && scrollY) {
Expand Down Expand Up @@ -509,14 +515,17 @@ const _ScrollView = forwardRef<HandlerRef<ScrollView & View, ScrollViewProps>, S
}, (refresherDefaultStyle && refresherDefaultStyle !== 'none' ? { colors: refreshColor[refresherDefaultStyle] } : null)))
: undefined
}),
wrapChildren(
props,
{
hasVarDec,
varContext: varContextRef.current,
textStyle,
textProps
}
createElement(ScrollViewContext.Provider,
{ value: contextValue },
wrapChildren(
props,
{
hasVarDec,
varContext: varContextRef.current,
textStyle,
textProps
}
)
)
)
})
Expand Down
Loading

0 comments on commit e748b09

Please sign in to comment.