Skip to content

Commit

Permalink
[example] Work on the examples
Browse files Browse the repository at this point in the history
  • Loading branch information
yamankatby committed Dec 27, 2021
1 parent 348735a commit 459d921
Show file tree
Hide file tree
Showing 28 changed files with 772 additions and 73 deletions.
1 change: 0 additions & 1 deletion core/src/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import Text from "./Text";
import ActivityIndicator from "./ActivityIndicator";
import Pressable, { PressableProps } from "./Pressable";
import { Color, usePaletteColor } from "./hooks/use-palette-color";
import { useTheme } from "./base/ThemeContext";
import { useSurfaceScale } from "./hooks/use-surface-scale";
import { useStyles } from "./hooks/use-styles";
import { useAnimatedElevation } from "./hooks/use-animated-elevation";
Expand Down
2 changes: 1 addition & 1 deletion core/src/Chip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const Chip: React.FC<ChipProps> = ({
leading,
trailing,
variant = "filled",
color = "onSurface",
color = "on-surface",
style,
contentContainerStyle,
labelStyle,
Expand Down
2 changes: 1 addition & 1 deletion core/src/DialogContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const DialogContent: React.FC<DialogContentProps> = ({ children }) => {

const scale = useMemo(
() => chroma.scale([backgroundColor, theme.palette.surface.on]),
[backgroundColor, theme.palette.onSurface],
[backgroundColor, theme.palette.surface.on],
);

return <View style={[styles.container, { borderColor: scale(0.08).hex() }]}>{children}</View>;
Expand Down
2 changes: 2 additions & 0 deletions core/src/Surface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export interface SurfaceProps extends Omit<ViewProps, "style"> {

/**
* The elevation of the surface.
*
* @default 0
*/
elevation?: Elevation;

Expand Down
14 changes: 12 additions & 2 deletions core/src/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@ import { TypographyVariant, useTheme } from "./base/ThemeContext";
import { Color, usePaletteColor } from "./hooks/use-palette-color";

export interface TextProps extends RNTextProps {
/**
* The variant of the text.
*
* @default "body1"
*/
variant?: TypographyVariant;

/**
* The color of the text.
*
* @default "on-background"
*/
color?: Color;
}

const Text: React.FC<TextProps> = ({ variant = "body1", color = "on-background", style, ...rest }) => {
const { typography } = useTheme();
const { main } = usePaletteColor(color);
const palette = usePaletteColor(color);

return <RNText {...rest} style={[typography[variant], { color: main }, style]} />;
return <RNText {...rest} style={[typography[variant], { color: palette.main }, style]} />;
};

export default Text;
14 changes: 8 additions & 6 deletions core/src/hooks/use-surface-color.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { useMemo } from "react";
import chroma from "chroma-js";
import { Elevation } from "../base/ThemeContext";
import { Elevation, useTheme } from "../base/ThemeContext";
import { useSurfaceScale } from "./use-surface-scale";

const domain = [0, 1, 2, 3, 4, 6, 8, 12, 16, 24].map(n => (n / 24) * 100);

export const useSurfaceColor = () => {
const { colorScheme } = useTheme();
const surfaceScale = useSurfaceScale();

return useMemo(
() =>
chroma.scale([0, 0.05, 0.07, 0.08, 0.09, 0.11, 0.12, 0.14, 0.15, 0.1].map(c => surfaceScale(c))).domain(domain),
[surfaceScale],
);
return useMemo(() => {
if (colorScheme === "light") return chroma.scale([surfaceScale(0)]);
return chroma
.scale([0, 0.05, 0.07, 0.08, 0.09, 0.11, 0.12, 0.14, 0.15, 0.1].map(c => surfaceScale(c)))
.domain(domain);
}, [colorScheme, surfaceScale]);
};

export const useSurfaceColorValue = (elevation: Elevation) => {
Expand Down
4 changes: 0 additions & 4 deletions example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { SafeAreaProvider, useSafeAreaInsets } from "react-native-safe-area-cont
import { DarkTheme, DefaultTheme, NavigationContainer, useNavigation } from "@react-navigation/native";
import { createNativeStackNavigator, NativeStackHeaderProps } from "@react-navigation/native-stack";
import { StatusBar } from "expo-status-bar";
import ActivityIndicatorScreen from "./src/ActivityIndicator";
import AppBarScreen from "./src/AppBar";
import AvatarScreen from "./src/Avatar";
import BackdropScreen from "./src/Backdrop";
Expand All @@ -22,12 +21,10 @@ import ListItemScreen from "./src/ListItem";
import PressableScreen from "./src/Pressable";
import SnackbarScreen from "./src/Snackbar";
import SurfaceScreen from "./src/Surface";
import SwitchScreen from "./src/Switch";
import TextScreen from "./src/Text";
import TextInputScreen from "./src/TextInput";

const screens = [
{ name: "Activity Indicator", component: ActivityIndicatorScreen },
{ name: "App Bar", component: AppBarScreen, options: { headerShown: false } },
{ name: "Avatar", component: AvatarScreen },
{ name: "Backdrop", component: BackdropScreen, options: { headerShown: false } },
Expand All @@ -43,7 +40,6 @@ const screens = [
{ name: "Pressable", component: PressableScreen },
{ name: "Snackbar", component: SnackbarScreen },
{ name: "Surface", component: SurfaceScreen },
{ name: "Switch", component: SwitchScreen },
{ name: "Text", component: TextScreen },
{ name: "Text Input", component: TextInputScreen },
];
Expand Down
2 changes: 2 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@react-native-material/core": "^1.3.0",
"@react-navigation/native": "^6.0.6",
"@react-navigation/native-stack": "^6.2.5",
"chroma-js": "^2.1.2",
"expo": "~44.0.0",
"expo-status-bar": "~1.2.0",
"react": "17.0.1",
Expand All @@ -24,6 +25,7 @@
},
"devDependencies": {
"@babel/core": "^7.12.9",
"@types/chroma-js": "^2.1.3",
"@types/react": "~17.0.21",
"@types/react-native": "~0.64.12",
"typescript": "~4.3.5"
Expand Down
7 changes: 0 additions & 7 deletions example/src/ActivityIndicator.tsx

This file was deleted.

232 changes: 229 additions & 3 deletions example/src/AppBar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,233 @@
import React from "react";
import React, { useState } from "react";
import { AppBar, Color, HStack, Icon, IconButton, ListItem, Switch, useTheme } from "@react-native-material/core";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import { Platform, View } from "react-native";

const AppBarScreen: React.FC = () => {
return null;
const AppBarScreen = ({ navigation }: any) => {
const theme = useTheme();
const insets = useSafeAreaInsets();

const [variant, setVariant] = useState<"top" | "bottom">("top");
const [titleShown, setTitleShown] = useState(true);
const [subtitleShown, setSubtitleShown] = useState(false);
const [menuIconShown, setMenuIconShown] = useState(false);
const [backIconShown, setBackIconShown] = useState(true);
const [heartIconShown, setHeartIconShown] = useState(true);
const [searchIconShown, setSearchIconShown] = useState(true);
const [dotsIconShown, setDotsIconShown] = useState(true);
const [centerTitle, setCenterTitle] = useState(false);
const [color, setColor] = useState("primary");
const [tintColor, setTintColor] = useState<Color | undefined>(undefined);
const [transparent, setTransparent] = useState(false);
const [enableColorOnDark, setEnableColorOnDark] = useState(false);

return (
<View style={{ flex: 1, paddingTop: variant === "bottom" ? insets.top : 0 }}>
<AppBar
variant={variant}
title={titleShown ? "App Bar" : undefined}
subtitle={subtitleShown ? "Lorem ipsum dolor." : undefined}
leading={props =>
(menuIconShown || backIconShown) && (
<HStack>
{menuIconShown && <IconButton icon={props => <Icon name="menu" {...props} />} {...props} />}
{backIconShown && (
<IconButton
icon={props => <Icon name={Platform.OS === "ios" ? "chevron-left" : "arrow-left"} {...props} />}
onPress={navigation.goBack}
{...props}
/>
)}
</HStack>
)
}
trailing={props => (
<HStack>
{heartIconShown && <IconButton icon={props => <Icon name="heart" {...props} />} {...props} />}
{searchIconShown && <IconButton icon={props => <Icon name="magnify" {...props} />} {...props} />}
{dotsIconShown && (
<IconButton
icon={props => <Icon name={Platform.OS === "ios" ? "dots-horizontal" : "dots-vertical"} {...props} />}
{...props}
/>
)}
</HStack>
)}
centerTitle={centerTitle}
color={color}
tintColor={tintColor}
transparent={transparent}
enableColorOnDark={enableColorOnDark}
style={
variant === "top"
? { paddingTop: insets.top, zIndex: 4 }
: {
position: "absolute",
bottom: 0,
start: 0,
end: 0,
paddingBottom: insets.bottom,
zIndex: 8,
}
}
/>

<ListItem
title="Change variant"
meta={variant === "top" ? "Top" : "Bottom"}
onPress={() => {
setVariant(variant === "top" ? "bottom" : "top");
}}
/>
<ListItem
title="Show title"
trailing={<Switch value={titleShown} onValueChange={() => setTitleShown(!titleShown)} />}
onPress={() => {
setTitleShown(!titleShown);
}}
/>
<ListItem
title="Show subtitle"
trailing={<Switch value={subtitleShown} onValueChange={() => setSubtitleShown(!subtitleShown)} />}
onPress={() => {
setSubtitleShown(!subtitleShown);
}}
/>
<ListItem
title="Leading icon"
trailing={props => (
<HStack style={{ marginEnd: 40 }}>
<IconButton
icon={props => <Icon name="menu" {...props} />}
onPress={() => {
setMenuIconShown(true);
setBackIconShown(false);
}}
{...props}
/>
<IconButton
icon={props => <Icon name={Platform.OS === "ios" ? "chevron-left" : "arrow-left"} {...props} />}
onPress={() => {
setMenuIconShown(false);
setBackIconShown(true);
}}
{...props}
/>
</HStack>
)}
/>
<ListItem
title="Trailing icon"
trailing={props => (
<HStack style={{ marginEnd: 88 }}>
<IconButton
icon={props => <Icon name="heart" {...props} />}
onPress={() => {
setHeartIconShown(!heartIconShown);
}}
{...props}
/>
<IconButton
icon={props => <Icon name="magnify" {...props} />}
onPress={() => {
setSearchIconShown(!searchIconShown);
}}
{...props}
/>
<IconButton
icon={props => <Icon name={Platform.OS === "ios" ? "dots-horizontal" : "dots-vertical"} {...props} />}
onPress={() => {
setDotsIconShown(!dotsIconShown);
}}
{...props}
/>
</HStack>
)}
/>
<ListItem
title="Center title"
trailing={<Switch value={centerTitle} onValueChange={() => setCenterTitle(!centerTitle)} />}
onPress={() => {
setCenterTitle(!centerTitle);
}}
/>
<ListItem
title="Color"
trailing={props => (
<HStack style={{ marginEnd: 88 }}>
<IconButton
icon={props => <Icon name="circle" {...props} color={theme.palette.primary.main} />}
onPress={() => {
setColor("primary");
}}
/>
<IconButton
icon={props => <Icon name="circle" {...props} color={theme.palette.secondary.main} />}
onPress={() => {
setColor("secondary");
}}
/>
<IconButton
icon={props => (
<Icon
name="swap-horizontal"
{...props}
color={color === "primary" || color === "secondary" ? props.color : color}
/>
)}
onPress={() => {
const random = (min: number, max: number) => Math.floor(Math.random() * (max - min + 1) + min);
setColor([theme.palette.error.main, "#26a69a", "#ff5722", "#FFFFFF", "#000000"][random(0, 7)]);
}}
{...props}
/>
</HStack>
)}
/>
<ListItem
title="Tint color"
trailing={props => (
<HStack style={{ marginEnd: 88 }}>
<IconButton
icon={props => <Icon name="circle" {...props} color="lightgray" />}
onPress={() => {
setTintColor("white");
}}
{...props}
/>
<IconButton
icon={props => <Icon name="circle" {...props} color="darkgray" />}
onPress={() => {
setTintColor("black");
}}
{...props}
/>
<IconButton
icon={props => <Icon name="auto-fix" {...props} />}
onPress={() => {
setTintColor(undefined);
}}
{...props}
/>
</HStack>
)}
/>
<ListItem
title="Transparent"
trailing={<Switch value={transparent} onValueChange={() => setTransparent(!transparent)} />}
onPress={() => {
setTransparent(!transparent);
}}
/>
<ListItem
title="Enable color on dark"
trailing={<Switch value={enableColorOnDark} onValueChange={() => setEnableColorOnDark(!enableColorOnDark)} />}
onPress={() => {
setEnableColorOnDark(!enableColorOnDark);
}}
/>
</View>
);
};

export default AppBarScreen;
Loading

2 comments on commit 459d921

@vercel
Copy link

@vercel vercel bot commented on 459d921 Dec 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 459d921 Dec 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.