Skip to content

Commit

Permalink
fix: improve header component to center the title properly
Browse files Browse the repository at this point in the history
  • Loading branch information
RyukTheCoder authored and yeager-eren committed Sep 1, 2024
1 parent f332672 commit a9929bb
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 20 deletions.
20 changes: 9 additions & 11 deletions widget/embedded/src/components/Layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,15 @@ function Layout(props: PropsWithChildren<PropTypes>) {
ref={containerRef}>
<Header
prefix={
<>
{showBackButton && (
<BackButton
onClick={() => {
navigateBack();
// As an example, used in routes page to add a custom logic when navigating back to the home page.
header.onBack && header.onBack();
}}
/>
)}
</>
showBackButton ? (
<BackButton
onClick={() => {
navigateBack();
// As an example, used in routes page to add a custom logic when navigating back to the home page.
header.onBack && header.onBack();
}}
/>
) : null
}
title={header.title}
suffix={
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Meta, StoryObj } from '@storybook/react';

import { Header } from './Header.js';
import { Header } from '@rango-dev/ui';

const meta: Meta<typeof Header> = {
component: Header,
Expand Down
25 changes: 24 additions & 1 deletion widget/ui/src/components/Header/Header.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export const globalHeaderStyles = globalCss({

export const Container = styled('div', {
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
padding: '$20 $20 $15 $20',
$$color: '$colors$neutral100',
Expand All @@ -24,6 +23,20 @@ export const Container = styled('div', {
backgroundColor: '$$color',
position: 'relative',

variants: {
titlePosition: {
left: {
justifyContent: 'start',
},
center: {
justifyContent: 'center',
},
right: {
justifyContent: 'end',
},
},
},

'.rng-curve-left,.rng-curve-right': {
width: HEADER_CORNDER_RADIUS * 2,
height: HEADER_CORNDER_RADIUS * 2,
Expand Down Expand Up @@ -59,7 +72,17 @@ export const Container = styled('div', {
},
});

export const Prefix = styled('div', {
position: 'absolute',
left: '$20',
display: 'flex',
alignItems: 'center',
gap: '$5',
});

export const Suffix = styled('div', {
position: 'absolute',
right: '$20',
display: 'flex',
alignItems: 'center',
gap: '$5',
Expand Down
33 changes: 27 additions & 6 deletions widget/ui/src/components/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import React from 'react';

import { Typography } from '../Typography/index.js';

import { Container, globalHeaderStyles, Suffix } from './Header.styles.js';
import { Container, globalHeaderStyles, Prefix, Suffix } from './Header.styles';

export function Header({
prefix,
Expand All @@ -14,12 +14,33 @@ export function Header({
}: PropsWithChildren<PropTypes>) {
globalHeaderStyles();

const getTitlePosition = () => {
if (!prefix) {
return 'left';
}
if (!suffix) {
return 'right';
}
return 'center';
};

const renderTitle = () => {
if (!title) {
return null;
} else if (typeof title === 'string') {
return (
<Typography variant="headline" size="small">
{title}
</Typography>
);
}
return title;
};

return (
<Container>
{prefix}
<Typography variant="headline" size="small">
{title}
</Typography>
<Container titlePosition={getTitlePosition()}>
<Prefix>{prefix}</Prefix>
{renderTitle()}
<Suffix>{suffix}</Suffix>
<div className="rng-curve-left"></div>
<div className="rng-curve-right"></div>
Expand Down
2 changes: 1 addition & 1 deletion widget/ui/src/components/Header/Header.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface PropTypes {
prefix?: React.ReactNode;
suffix?: React.ReactNode;
title: string;
title?: string | React.ReactNode;
}

0 comments on commit a9929bb

Please sign in to comment.