Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(view): view hover style #1769

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 })
62 changes: 13 additions & 49 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 } from 'react-native-gesture-handler'
import { getCurrentPage, splitProps, splitStyle, useLayout, useTransformStyle, wrapChildren, extendObject, useHoverStyle } 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,15 @@ 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 { isHover, enableHoverStyle, gesture } = useHoverStyle({ hoverStyle, hoverStartTime, hoverStayTime, disabled })

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

Expand All @@ -233,16 +233,6 @@ 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'
Expand Down Expand Up @@ -366,34 +356,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 +380,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 +402,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 +413,10 @@ const Button = forwardRef<HandlerRef<View, ButtonProps>, ButtonProps>((buttonPro
}
)
)

return enableHoverStyle
? createElement(GestureDetector, { gesture }, 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 @@ -507,14 +513,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
Loading