Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

refactor button

ammarahm-ed15638b495a2d
diff --git a/apps/mobile/src/components/Button/index.js b/apps/mobile/src/components/Button/index.js
index b6d7de2c9..e7ff2445d 100644
--- a/apps/mobile/src/components/Button/index.js
+++ b/apps/mobile/src/components/Button/index.js
@@ -1,47 +1,16 @@
import React from 'react';
-import {ActivityIndicator, StyleSheet, Text} from 'react-native';
+import { ActivityIndicator } from 'react-native';
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
-import {useTracked} from '../../provider';
-import {PressableButton} from '../PressableButton';
-import {ph, pv, SIZE, WEIGHT} from '../../utils/SizeUtils';
-import Paragraph from '../Typography/Paragraph';
+import { useTracked } from '../../provider';
+import { BUTTON_TYPES } from '../../utils';
+import { ph, pv, SIZE } from '../../utils/SizeUtils';
+import { PressableButton } from '../PressableButton';
import Heading from '../Typography/Heading';
-const BUTTON_TYPES = {
- transparent: {
- primary: 'transparent',
- text: 'accent',
- selected: 'shade',
- },
- gray: {
- primary: 'transparent',
- text: 'icon',
- selected: 'nav',
- },
- grayBg: {
- primary: 'nav',
- text: 'icon',
- selected: 'nav',
- },
- accent: {
- primary: 'accent',
- text: 'light',
- selected: 'accent',
- },
- inverted: {
- primary: 'bg',
- text: 'accent',
- selected: 'bg',
- },
-
- shade: {
- primary: 'shade',
- text: 'accent',
- selected: 'accent',
- opacity: 0.12,
- },
-};
-
+/**
+ *
+ * @param {import('../PressableButton').buttonTypes} type
+ */
export const Button = ({
height = 40,
width = null,
@@ -53,20 +22,27 @@ export const Button = ({
type = 'transparent',
iconSize = SIZE.md,
style = {},
- testID
+ testID,
+ accentColor = 'accent',
+ accentText = 'light',
}) => {
const [state] = useTracked();
const {colors} = state;
+ const textColor =
+ colors[
+ type === 'accent'
+ ? BUTTON_TYPES[type](accentColor, accentText).text
+ : BUTTON_TYPES[type].text
+ ];
return (
<PressableButton
onPress={onPress}
disabled={loading}
testID={testID}
- color={colors[BUTTON_TYPES[type].primary]}
- selectedColor={colors[BUTTON_TYPES[type].selected]}
- alpha={!colors.night ? -0.04 : 0.04}
- opacity={BUTTON_TYPES[type].opacity || 1}
+ type={type}
+ accentColor={accentColor}
+ accentText={accentText}
customStyle={{
height: height,
width: width || null,
@@ -79,20 +55,20 @@ export const Button = ({
flexDirection: 'row',
...style,
}}>
- {loading && <ActivityIndicator color={colors[BUTTON_TYPES[type].text]} />}
+ {loading && <ActivityIndicator color={textColor} />}
{icon && !loading && (
<Icon
name={icon}
style={{
marginRight: 0,
}}
- color={colors[BUTTON_TYPES[type].text]}
+ color={textColor}
size={iconSize}
/>
)}
{!title ? null : (
<Heading
- color={colors[BUTTON_TYPES[type].text]}
+ color={textColor}
size={fontSize}
style={{
marginLeft: icon || loading ? 5 : 0,
@@ -102,15 +78,4 @@ export const Button = ({
)}
</PressableButton>
);
-};
-
-const styles = StyleSheet.create({
- activityText: {
- fontSize: SIZE.sm,
- textAlign: 'center',
- },
- activityContainer: {
- alignItems: 'center',
- justifyContent: 'center',
- },
-});
+};
\ No newline at end of file
diff --git a/apps/mobile/src/components/PressableButton/index.js b/apps/mobile/src/components/PressableButton/index.js
index 466f139c3..c2c64b195 100644
--- a/apps/mobile/src/components/PressableButton/index.js
+++ b/apps/mobile/src/components/PressableButton/index.js
@@ -1,47 +1,75 @@
-import React from 'react';
-import {Pressable} from 'react-native';
-import {hexToRGBA, RGB_Linear_Shade} from "../../utils/ColorUtils";
-import {br} from "../../utils/SizeUtils";
+import React, { useCallback } from 'react';
+import { Pressable } from 'react-native';
+import { useTracked } from '../../provider';
+import { BUTTON_TYPES } from '../../utils';
+import { hexToRGBA, RGB_Linear_Shade } from '../../utils/ColorUtils';
+import { br } from '../../utils/SizeUtils';
+
+/**
+ *
+ * @typedef {Object} buttonTypes
+ * @property {"transparent" | "gray" | "grayBg" | "accent" | "inverted" | "shade"} type type of button
+ *
+ *@param {buttonTypes} type
+ *
+ */
export const PressableButton = ({
- color,
- selectedColor,
- borderless,
- radius,
children,
onPress,
customStyle,
- alpha = -0.1,
- opacity = 1,
onLongPress,
hitSlop,
testID,
- disabled
+ disabled,
+ type = 'gray',
+ noborder,
+ accentColor = 'accent',
+ accentText = 'light',
}) => {
+ const [state] = useTracked();
+ const {colors} = state;
+ const selectedColor =
+ colors[
+ type === 'accent'
+ ? BUTTON_TYPES[type](accentColor, accentText).selected
+ : BUTTON_TYPES[type].selected
+ ];
+ const primaryColor =
+ colors[
+ type === 'accent'
+ ? BUTTON_TYPES[type](accentColor, accentText).primary
+ : BUTTON_TYPES[type].primary
+ ];
+ const opacity = type === 'accent' ? 1 : BUTTON_TYPES[type].opacity;
+ const alpha = colors.night ? 0.04 : -0.04;
+
+ const getStyle = useCallback(
+ ({pressed}) => [
+ {
+ backgroundColor: pressed
+ ? RGB_Linear_Shade(alpha, hexToRGBA(selectedColor, opacity || 1))
+ : hexToRGBA(primaryColor, opacity || 1 - 0.02),
+ width: '100%',
+ alignSelf: 'center',
+ borderRadius: noborder ? 0 : br,
+ justifyContent: 'center',
+ alignItems: 'center',
+ marginBottom: 0,
+ },
+ customStyle,
+ ],
+ [customStyle, noborder, type],
+ );
+
return (
<Pressable
testID={testID}
disabled={disabled}
hitSlop={hitSlop}
- activeOpacity={opacity}
onPress={onPress}
onLongPress={onLongPress}
- style={({pressed}) => [
- {
- backgroundColor: pressed
- ? RGB_Linear_Shade(alpha, hexToRGBA(selectedColor, opacity))
- : color !== 'transparent'
- ? hexToRGBA(color, opacity - 0.02)
- : color,
- width: '100%',
- alignSelf: 'center',
- borderRadius: br,
- justifyContent: 'center',
- alignItems: 'center',
- marginBottom: 0,
- },
- customStyle,
- ]}>
+ style={getStyle}>
{children}
</Pressable>
);