-
-
Notifications
You must be signed in to change notification settings - Fork 63
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
WIP|Implement for react-native-web #112
base: master
Are you sure you want to change the base?
Changes from 13 commits
dde7d52
dbb5e91
133aae7
22bacde
5dbb7af
d167339
67c70b1
56d8196
adf71c5
318d43c
e9d4d2c
c27164d
f81736f
bada2a4
ce59868
ea7c389
6617368
9ac4819
af2a8f9
7140010
9d93f26
cd22852
3720989
777befb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,3 +57,6 @@ buck-out/ | |
|
||
# CocoaPods | ||
/ios/Pods/ | ||
|
||
# Expo | ||
.expo/ |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { NativeModules, Platform } from 'react-native'; | ||
const BlurhashModule: BlurhashModule = Platform.select({ | ||
web: null, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO |
||
native: NativeModules.BlurhashModule, | ||
}); | ||
|
||
type BlurhashModule = { | ||
createBlurhashFromImage: (imageUri: string, componentsX: number, componentsY: number) => Promise<string>, | ||
clearCosineCache: () => void | ||
}; | ||
|
||
export default BlurhashModule; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
import * as React from 'react'; | ||
import { requireNativeComponent, NativeModules, Platform, ViewProps, NativeSyntheticEvent } from 'react-native'; | ||
import { requireNativeComponent, Platform, ViewProps, NativeSyntheticEvent } from 'react-native'; | ||
import { decode83, decodeDC, isBlurhashValid, RGB } from './utils'; | ||
|
||
// NativeModules automatically resolves 'BlurhashView' to 'BlurhashViewModule' | ||
const BlurhashModule = NativeModules.BlurhashView; | ||
import BlurhashModule from './blurhashModule'; | ||
|
||
export interface BlurhashProps extends Omit<ViewProps, 'children'> { | ||
/** | ||
|
@@ -123,7 +122,10 @@ export class Blurhash extends React.PureComponent<BlurhashProps> { | |
if (this.props.onLoadEnd != null) this.props.onLoadEnd(); | ||
} | ||
_onLoadError(event?: NativeSyntheticEvent<{ message?: string }>) { | ||
if (this.props.onLoadError != null) this.props.onLoadError(event?.nativeEvent?.message); | ||
if (this.props.onLoadError != null) { | ||
const message = event?.nativeEvent?.message; // TODO: Not sure how to get proper value here on web | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO |
||
this.props.onLoadError(message); | ||
} | ||
} | ||
|
||
render() { | ||
|
@@ -132,16 +134,14 @@ export class Blurhash extends React.PureComponent<BlurhashProps> { | |
{...this.props} | ||
onLoadStart={this._onLoadStart} | ||
onLoadEnd={this._onLoadEnd} | ||
// @ts-expect-error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was giving me a build error |
||
onLoadError={this._onLoadError} | ||
/> | ||
); | ||
} | ||
} | ||
|
||
// requireNativeComponent automatically resolves 'BlurhashView' to 'BlurhashViewManager' | ||
const NativeBlurhashView = requireNativeComponent<BlurhashProps>( | ||
'BlurhashView', | ||
// @ts-expect-error this second argument is still not public, but probably required for TurboModules. | ||
Blurhash, | ||
); | ||
const NativeBlurhashView = Platform.OS === 'web' ? require('./web/blurhashView').default : requireNativeComponent<BlurhashProps>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried a couple iterations of this. Setting Using |
||
'BlurhashView', | ||
// @ts-expect-error this second argument is still not public, but probably required for TurboModules. | ||
Blurhash, | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { View } from 'react-native'; | ||
import BlurhashCanvas from './canvas'; | ||
import type { BlurhashProps } from '../index'; | ||
import { decode } from 'blurhash'; | ||
|
||
interface BlurhashViewProps extends BlurhashProps { | ||
} | ||
|
||
export default function BlurhashView ({ | ||
blurhash, | ||
decodeHeight = 128, | ||
decodePunch, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to test and ensure punch is implemented properly. |
||
decodeWidth = 128, | ||
onLoadStart = () => null, | ||
onLoadEnd = () => null, | ||
onLoadError = () => null, | ||
style, | ||
...rest | ||
}: BlurhashViewProps) { | ||
const [decodedBlurhash, setDecodedBlurhash] = useState<null | Uint8ClampedArray>(null); | ||
useEffect(() => { | ||
try { | ||
onLoadStart(); | ||
setDecodedBlurhash(decode(blurhash, decodeWidth, decodeHeight, decodePunch)); | ||
} catch (e) { | ||
onLoadError(e); | ||
} | ||
}, [blurhash]); | ||
|
||
return ( | ||
<View style={style}> | ||
{decodedBlurhash ? <BlurhashCanvas | ||
decodedBlurhash={decodedBlurhash} | ||
height={decodeHeight} | ||
onLoadEnd={onLoadEnd} | ||
width={decodeWidth} | ||
/> : null} | ||
</View> | ||
); | ||
}; |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,30 @@ | ||||
import React, { useCallback } from 'react'; | ||||
|
||||
export type Props = React.CanvasHTMLAttributes<HTMLCanvasElement> & { | ||||
getContext?: Function; | ||||
decodedBlurhash: Uint8ClampedArray; | ||||
height?: number; | ||||
onLoadEnd: Function; | ||||
punch?: number; | ||||
width?: number; | ||||
}; | ||||
|
||||
export default function BlurhashCanvas ({ | ||||
// TODO: See how canvas handles unset width/height | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
decodedBlurhash, | ||||
height = 128, | ||||
onLoadEnd, | ||||
width = 128, | ||||
...rest | ||||
}: Props) { | ||||
const canvasRef = useCallback((canvas: Props) => { | ||||
if (!canvas) return; | ||||
const ctx = canvas.getContext && canvas.getContext('2d'); | ||||
const imageData = ctx.createImageData(width, height); | ||||
imageData.data.set(decodedBlurhash); | ||||
ctx.putImageData(imageData, 0, 0); | ||||
onLoadEnd(); | ||||
}, [decodedBlurhash]); | ||||
|
||||
return <canvas {...rest} height={height} width={width} ref={canvasRef} /> | ||||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could not get the current iOS example to run without these changes.