Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cold-mice-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@equinor/mad-components": minor
---

Added `fullWidth` prop to the `Button` component
13 changes: 13 additions & 0 deletions apps/chronicles/screens/components/components/ButtonScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ export const ButtonScreen = () => {
<Button title="Disabled" variant="ghost" disabled />
</View>
<Spacer />

<Typography variant="h2">fullWidth</Typography>
<Typography>
With use of the fullWidth prop icon floats to the edges of the button while the text
stay centered:
</Typography>
<View style={{ gap: 12 }}>
<Button title="With fullwidth" iconName="dog" iconPosition="leading" fullWidth />
<Button title="With fullwidth" iconName="cat" iconPosition="trailing" fullWidth />
<Button title="without fullwidth" iconName="bird" />
</View>
<Spacer />

<View style={styles.buttonRow}>
<Button title="Loading" loading />
<Button title="Loading" loading disabled />
Expand Down
26 changes: 23 additions & 3 deletions packages/components/src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export type ButtonSpecificProps = {
* Boolean value indicating whether or not the button should be in its loading state.
*/
loading?: boolean;
/**
* Boolean value that floats icon to the edges of the button while the text stay centered.
*/
fullWidth?: boolean;
/**
* Name of the icon to use with the title.
*/
Expand Down Expand Up @@ -63,6 +67,7 @@ export const Button = forwardRef<View, ButtonProps>(
variant = "contained",
disabled = false,
loading = false,
fullWidth = false,
iconName,
iconPosition = "leading",
onPress = () => null,
Expand All @@ -83,18 +88,23 @@ export const Button = forwardRef<View, ButtonProps>(
toggleStatus: isToggleButton ? toggleData.isSelected : false,
groupData,
disabled,
fullWidth,
});

const ButtonContent = () => (
<>
{iconName && iconPosition === "leading" && (
<Icon name={iconName} color={styles.textStyle.color} />
<View style={styles.leadingIcon}>
<Icon name={iconName} color={styles.textStyle.color} />
</View>
)}
<Typography group="interactive" variant="button" style={styles.textStyle}>
{title}
</Typography>
{iconName && iconPosition === "trailing" && (
<Icon name={iconName} color={styles.textStyle.color} />
<View style={styles.trailingIcon}>
<Icon name={iconName} color={styles.textStyle.color} />
</View>
)}
</>
);
Expand Down Expand Up @@ -133,10 +143,11 @@ type ButtonStyleSheetProps = {
color: "primary" | "secondary" | "danger";
variant: "contained" | "outlined" | "ghost";
disabled: boolean;
fullWidth: boolean;
};

const themeStyles = EDSStyleSheet.create((theme, props: ButtonStyleSheetProps) => {
const { color, isToggleButton, toggleStatus, groupData, disabled } = props;
const { color, isToggleButton, toggleStatus, groupData, disabled, fullWidth } = props;
let { variant } = props;

variant = isToggleButton ? (toggleStatus ? "contained" : "outlined") : variant;
Expand Down Expand Up @@ -175,8 +186,17 @@ const themeStyles = EDSStyleSheet.create((theme, props: ButtonStyleSheetProps) =
justifyContent: "center",
gap: theme.spacing.button.iconGap,
},
trailingIcon: {
flex: fullWidth ? 1 : undefined,
alignItems: fullWidth ? "flex-end" : undefined,
},
leadingIcon: {
flex: fullWidth ? 1 : undefined,
alignItems: fullWidth ? "flex-start" : undefined,
},
textStyle: {
color: textColor,
position: fullWidth ? "absolute" : undefined,
},
};
});
Loading