Skip to content

Commit

Permalink
Add ability to change header type during fast-refresh/live, moar exam…
Browse files Browse the repository at this point in the history
…ples
  • Loading branch information
tboba committed Jan 8, 2024
1 parent 7168134 commit 144ddf2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
13 changes: 12 additions & 1 deletion FabricTestExample/src/Test1874.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ function useHeaderOptions(
}

function SettingsSwitcher({ navigation }: SettingsScreenProps) {
const { currentValue: headerType, component: headerTypeSetting } =
useHeaderOptions('header type', 'large', ['large', 'medium', 'small']);
const { currentValue: headerTitleAlign, component: headerTitleAlignSetting } =
useHeaderOptions('title align', 'left', ['left', 'center']);
const { currentValue: backgroundColor, component: bgColorSetting } =
useHeaderOptions('background color', 'white', [
'white',
Expand Down Expand Up @@ -138,21 +142,27 @@ function SettingsSwitcher({ navigation }: SettingsScreenProps) {
navigation.setOptions({
headerStyle: { backgroundColor },
headerTitleStyle: { color: textColor },
headerTitleAlign,
headerHideShadow: hideShadow,
headerShown,
headerTranslucent,
headerType,
});
}, [
navigation,
headerType,
backgroundColor,
textColor,
hideShadow,
headerShown,
headerTranslucent,
headerTitleAlign,
]);

return (
<View style={styles.settingSection}>
{headerTypeSetting}
{headerTitleAlignSetting}
{bgColorSetting}
{textColorSetting}
{hideShadowSetting}
Expand Down Expand Up @@ -274,7 +284,8 @@ export default function App() {
name="Settings"
component={Settings}
options={{
headerType: 'large',
headerTitle: 'Settings',
headerType: 'medium',
}}
/>
<Stack.Screen
Expand Down
13 changes: 12 additions & 1 deletion TestsExample/src/Test1874.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ function useHeaderOptions(
}

function SettingsSwitcher({ navigation }: SettingsScreenProps) {
const { currentValue: headerType, component: headerTypeSetting } =
useHeaderOptions('header type', 'large', ['large', 'medium', 'small']);
const { currentValue: headerTitleAlign, component: headerTitleAlignSetting } =
useHeaderOptions('title align', 'left', ['left', 'center']);
const { currentValue: backgroundColor, component: bgColorSetting } =
useHeaderOptions('background color', 'white', [
'white',
Expand Down Expand Up @@ -138,21 +142,27 @@ function SettingsSwitcher({ navigation }: SettingsScreenProps) {
navigation.setOptions({
headerStyle: { backgroundColor },
headerTitleStyle: { color: textColor },
headerTitleAlign,
headerHideShadow: hideShadow,
headerShown,
headerTranslucent,
headerType,
});
}, [
navigation,
headerType,
backgroundColor,
textColor,
hideShadow,
headerShown,
headerTranslucent,
headerTitleAlign,
]);

return (
<View style={styles.settingSection}>
{headerTypeSetting}
{headerTitleAlignSetting}
{bgColorSetting}
{textColorSetting}
{hideShadowSetting}
Expand Down Expand Up @@ -274,7 +284,8 @@ export default function App() {
name="Settings"
component={Settings}
options={{
headerType: 'large',
headerTitle: 'Settings',
headerType: 'medium',
}}
/>
<Stack.Screen
Expand Down
7 changes: 6 additions & 1 deletion android/src/main/java/com/swmansion/rnscreens/Screen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class Screen(context: ReactContext?) : FabricEnabledViewGroup(context) {
var isGestureEnabled = true
var screenOrientation: Int? = null
private set
var headerType = HeaderType.Small
var isStatusBarAnimated: Boolean? = null

init {
Expand Down Expand Up @@ -205,6 +204,12 @@ class Screen(context: ReactContext?) : FabricEnabledViewGroup(context) {
fragmentWrapper?.let { ScreenWindowTraits.setColor(this, it.tryGetActivity(), it.tryGetContext()) }
}

var headerType = HeaderType.Small
set(headerType) {
field = headerType
headerConfig?.onUpdate()
}

var navigationBarColor: Int? = null
set(navigationBarColor) {
if (navigationBarColor != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.google.android.material.R as MaterialR
class ScreenStackHeader(val screen: Screen) {
var toolbar: CustomToolbar? = null
var collapsingToolbarLayout: CollapsingToolbarLayout? = null
var loadedHeaderType = screen.headerType

fun recreateToolbar() {
if (screen.headerType.isCollapsing) {
Expand All @@ -28,6 +29,10 @@ class ScreenStackHeader(val screen: Screen) {
}
}

fun updateHeaderType() {
loadedHeaderType = screen.headerType
}

fun removeToolbar() {
toolbar?.removeAllViews()
collapsingToolbarLayout?.removeAllViews()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ class ScreenStackHeaderConfig(context: Context) : ViewGroup(context) {
return
}

// Reattach the header, when the developer tries to change the header type
if (screenStackHeader?.loadedHeaderType != screen?.headerType) {
screenStackHeader?.updateHeaderType()
screenFragment?.removeToolbar()
}

if (toolbar.parent == null) {
screenFragment?.setToolbar(toolbar)
}
Expand Down

0 comments on commit 144ddf2

Please sign in to comment.