diff --git a/android/src/main/java/com/urbanairship/reactnative/ReactEmbeddedView.kt b/android/src/main/java/com/urbanairship/reactnative/ReactEmbeddedView.kt index a813ab30..02b5c6db 100644 --- a/android/src/main/java/com/urbanairship/reactnative/ReactEmbeddedView.kt +++ b/android/src/main/java/com/urbanairship/reactnative/ReactEmbeddedView.kt @@ -12,6 +12,12 @@ import com.urbanairship.json.JsonValue class ReactEmbeddedView(context: Context) : FrameLayout(context) { private var renderedConfig: String? = null + private var sizeToContent: Boolean = false + + /** Records whether the view measures its own content rather than filling its box. */ + fun setSizeToContent(value: Boolean) { + this.sizeToContent = value + } fun setConfig(config: String?) { if (config == null || config == renderedConfig) { @@ -56,4 +62,8 @@ class ReactEmbeddedView(context: Context) : FrameLayout(context) { MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)) layout(left, top, right, bottom) } + + companion object { + const val EVENT_SIZE_CHANGE = "topAirshipEmbeddedViewSizeChange" + } } diff --git a/android/src/main/java/com/urbanairship/reactnative/ReactEmbeddedViewManager.kt b/android/src/main/java/com/urbanairship/reactnative/ReactEmbeddedViewManager.kt index 284f689d..5cafc3a0 100644 --- a/android/src/main/java/com/urbanairship/reactnative/ReactEmbeddedViewManager.kt +++ b/android/src/main/java/com/urbanairship/reactnative/ReactEmbeddedViewManager.kt @@ -18,6 +18,7 @@ class ReactEmbeddedViewManager : SimpleViewManager(), override fun setProperty(view: ReactEmbeddedView, propName: String, value: Any?) { when (propName) { "config" -> setConfig(view, value as? String) + "sizeToContent" -> setSizeToContent(view, value as? Boolean ?: false) else -> {} } } @@ -44,11 +45,27 @@ class ReactEmbeddedViewManager : SimpleViewManager(), return ReactEmbeddedView(reactContext) } + override fun getExportedCustomBubblingEventTypeConstants(): MutableMap { + return mutableMapOf( + ReactEmbeddedView.EVENT_SIZE_CHANGE to mapOf( + "phasedRegistrationNames" to mapOf( + "bubbled" to "onSizeChange", + "captured" to "onSizeChangeCapture" + ) + ) + ) + } + @ReactProp(name = "config") override fun setConfig(view: ReactEmbeddedView, config: String?) { view.setConfig(config) } + @ReactProp(name = "sizeToContent") + override fun setSizeToContent(view: ReactEmbeddedView, sizeToContent: Boolean) { + view.setSizeToContent(sizeToContent) + } + companion object { const val REACT_CLASS = "RNAirshipEmbeddedView" } diff --git a/src/RNAirshipEmbeddedViewNativeComponent.ts b/src/RNAirshipEmbeddedViewNativeComponent.ts index 616fc9d5..99e9ee23 100644 --- a/src/RNAirshipEmbeddedViewNativeComponent.ts +++ b/src/RNAirshipEmbeddedViewNativeComponent.ts @@ -1,8 +1,36 @@ // @ts-ignore import { codegenNativeComponent, type HostComponent, type ViewProps } from 'react-native'; +import type { + BubblingEventHandler, + Double, + WithDefault, + // @ts-ignore +} from 'react-native/Libraries/Types/CodegenTypes'; + +type EmbeddedSizeChangeEvent = Readonly<{ + /** + * The content's measured height, in points. + */ + height: Double; +}>; interface NativeProps extends ViewProps { config: string; + + /** + * Whether the view measures its own content and reports the height, rather + * than filling the box it is given. + */ + sizeToContent?: WithDefault; + + /** + * Called whenever the measured content height changes. Only emitted while + * `sizeToContent` is set. + */ + onSizeChange?: BubblingEventHandler< + EmbeddedSizeChangeEvent, + 'topAirshipEmbeddedViewSizeChange' + >; } export default codegenNativeComponent('RNAirshipEmbeddedView') as HostComponent;