Skip to content

Commit

Permalink
feat(navigation): covert sass vars to css vars. Add colors prop to co…
Browse files Browse the repository at this point in the history
…mponent

- add css custom properties
- user can now change basic colors with a new color object to override css vars
- add industrial strength string to kebab case util that handles numbers (into common)
  • Loading branch information
unleashit committed Jan 13, 2024
1 parent 1566e99 commit 70d90ac
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 37 deletions.
8 changes: 5 additions & 3 deletions packages/navigation/src/navigation.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import * as React from 'react';
import React from 'react';
import { DefaultLinkComponent, utils } from '@unleashit/common';
import AuthLinks from './AuthLinks';
import NavLinks from './NavLinks';
import NavContext from './NavContext';
import { addTemplateClasses, mapArrayToClasses } from './utils/generateClasses';
import { AuthLinkTypes, NavigationLink } from './types';
import { Colors, mapColorsToStyles } from './utils/mapColorsToStyles';

export interface NavigationProps {
links: NavigationLink[];
linkComponent?: React.ComponentType<any>;
linkComponentHrefAttr?: string;
direction?: 'horizontal' | 'vertical' | 'horz' | 'vert';
template?: 'clean' | 'dark-buttons' | 'light-buttons' | 'none';
colors?: Colors;
classes?: string[];
isAuth?: boolean;
authLinks?: AuthLinkTypes;
Expand Down Expand Up @@ -53,6 +55,7 @@ const Navigation = ({
links,
direction = 'horizontal',
template = 'clean',
colors,
classes,
isAuth,
authLinks,
Expand All @@ -71,8 +74,6 @@ const Navigation = ({
[cssModule],
);

console.log({ cssModule, clsName });

const contextValue = React.useMemo(
() => ({
linkComponent,
Expand All @@ -95,6 +96,7 @@ const Navigation = ({
}${addTemplateClasses(template, direction, clsName)}${mapArrayToClasses<
NavigationProps['classes']
>(classes)}`}
style={mapColorsToStyles(colors)}
>
<NavLinks links={links} clsName={clsName} />
{authSidecarLinks && (
Expand Down
55 changes: 33 additions & 22 deletions packages/navigation/src/scss/navigation.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
display: flex;
font-size: 1.1rem;
text-transform: uppercase;
--unl-navigation-light: #{$lightColor};
--unl-navigation-light-darker-5: #{darken($lightColor, 5%)};
--unl-navigation-light-darker-10: #{darken($lightColor, 10%)};
--unl-navigation-light-darker-15: #{darken($lightColor, 15%)};
--unl-navigation-dark: #{$darkColor};
--unl-navigation-dark-lighter-5: #{lighten($darkColor, 5%)};
--unl-navigation-dark-lighter-10: #{lighten($darkColor, 10%)};
--unl-navigation-dark-lighter-15: #{lighten($darkColor, 15%)};
--unl-navigation-text-light: #{$white};
--unl-navigation-text-dark: #{$black};
}

.container {
Expand All @@ -28,7 +38,7 @@
padding: 8px 20px;
align-items: center;
justify-content: center;
color: #000;
color: var(--unl-navigation-text-dark);
text-decoration: none;
cursor: pointer;
}
Expand Down Expand Up @@ -69,34 +79,35 @@
}
}
.link {
background-color: $darkColor;
color: #fff;
border-right: 1px solid lighten($darkColor, 15%);
background-color: var(--unl-navigation-dark);
color: var(--unl-navigation-text-light);
border-right: 1px solid var(--unl-navigation-dark-lighter-15);
&:hover {
background-color: lighten($darkColor, 10%);
background-color: var(--unl-navigation-dark-lighter-10);
}
}
.active .link {
background-color: $lightColor;
color: #000;
background-color: var(--unl-navigation-light);
color: var(--unl-navigation-text-dark);
}
&.vertical {
.link {
border-right: none;
border-bottom: 1px solid lighten($darkColor, 15%);
border-bottom: 1px solid var(--unl-navigation-dark-lighter-15);
}
}
.authLinks {
.link {
background-color: transparent;
border: none;
color: var(--unl-navigation-text-dark);
}
.signupLink,
.logoutLink {
background-color: $lightColor;
background-color: var(--unl-navigation-light);
border-radius: 0;
&:hover {
background-color: darken($lightColor, 5%);
background-color: var(--unl-navigation-light-darker-5);
}
}
}
Expand All @@ -111,21 +122,21 @@
}
}
.link {
background-color: $lightColor;
color: #000;
border-right: 1px solid darken($lightColor, 15%);
background-color: var(--unl-navigation-light);
color: var(--unl-navigation-text-dark);
border-right: 1px solid var(--unl-navigation-light-darker-15);
&:hover {
background-color: darken($lightColor, 5%);
background-color: var(--unl-navigation-light-darker-5);
}
}
.active .link {
background-color: $darkColor;
color: #fff;
background-color: var(--unl-navigation-dark);
color: var(--unl-navigation-text-light);
}
&.vertical {
.link {
border-right: none;
border-bottom: 1px solid darken($lightColor, 15%);
border-bottom: 1px solid var(--unl-navigation-light-darker-15);
}
}
.authLinks {
Expand All @@ -135,11 +146,11 @@
}
.signupLink,
.logoutLink {
background-color: $darkColor;
background-color: var(--unl-navigation-dark);
border-radius: 0;
color: #fff;
color: var(--unl-navigation-text-light);
&:hover {
background-color: lighten($darkColor, 10%);
background-color: var(--unl-navigation-dark-lighter-10);
}
}
}
Expand All @@ -159,10 +170,10 @@
}
.signupLink,
.logoutLink {
background-color: $lightColor;
background-color: var(--unl-navigation-light);
border-radius: 5px;
&:hover {
background-color: darken($lightColor, 5%);
background-color: var(--unl-navigation-light-darker-5);
}
}
.iconSpanLeft + .loginLink {
Expand Down
2 changes: 2 additions & 0 deletions packages/navigation/src/scss/vars.scss
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
$lightColor: #f1efef;
$darkColor: #555;
$white: #fff;
$black: #000;
72 changes: 72 additions & 0 deletions packages/navigation/src/utils/mapColorsToStyles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { CSSProperties } from 'react';
import { utils } from '@unleashit/common';

// export interface Colors {
// light?: CSSProperties['color'];
// lightDarker5?: CSSProperties['color'];
// lightDarker10?: CSSProperties['color'];
// lightDarker15?: CSSProperties['color'];
// dark?: CSSProperties['color'];
// darkLighter5?: CSSProperties['color'];
// darkLighter10?: CSSProperties['color'];
// darkLighter15?: CSSProperties['color'];
// textLight?: CSSProperties['color'];
// textDark?: CSSProperties['color'];
// }

// export const mapColorsToStyles = (colors?: Colors) => {
// const styles: Record<string, string> = {};
// if (!colors) return styles;
//
// colors.light && (styles['--unl-navigation-light'] = colors.light);
// colors.dark && (styles['--unl-navigation-dark'] = colors.dark);
// colors.textLight &&
// (styles['--unl-navigation-text-light'] = colors.textLight);
// colors.textDark && (styles['--unl-navigation-text-dark'] = colors.textDark);
// colors.lightDarker5 &&
// (styles['--unl-navigation-light-darker-5'] = colors.lightDarker5);
// colors.lightDarker10 &&
// (styles['--unl-navigation-light-darker-10'] = colors.lightDarker10);
// colors.lightDarker15 &&
// (styles['--unl-navigation-light-darker-15'] = colors.lightDarker15);
// colors.darkLighter5 &&
// (styles['--unl-navigation-dark-lighter-5'] = colors.darkLighter5);
// colors.darkLighter10 &&
// (styles['--unl-navigation-dark-lighter-10'] = colors.darkLighter10);
// colors.darkLighter15 &&
// (styles['--unl-navigation-dark-lighter-15'] = colors.darkLighter15);
// return styles;
// };

const colorKeys = [
'light',
'dark',
'textLight',
'textDark',
'lightDarker5',
'lightDarker10',
'lightDarker15',
'darkLighter5',
'darkLighter10',
'darkLighter15',
] as const;

export type Colors = {
[key in (typeof colorKeys)[number]]?: CSSProperties['color'];
};

export const mapColorsToStyles = (colors?: Colors) => {
if (!colors) return {};

const styles: Partial<Record<string, string>> = {};

colorKeys.forEach((key) => {
if (colors[key]) {
const kebabCaseKey = `--unl-navigation-${utils.stringToKebab(key)}`;

styles[kebabCaseKey] = colors[key];
}
});

return styles;
};
21 changes: 9 additions & 12 deletions shared/common/src/lib/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ export const camelToKebab = (str: string) =>
.replace(/\s/, '-')
.toLowerCase();

export const stringToKebab = (str: string) => {
const matches = str.match(
/[0-9]{1,}(?=\b)|[A-Z]{2,}(?=[A-Z][a-z]+|[0-9]|\b|_)|[A-Z]?[a-z]+|[A-Z]|[0-9]+/g,
);
if (!matches || matches.length === 0) return str;

return matches.map((x) => x.toLowerCase()).join('-');
};

export const convertToSentence = (str: string): string =>
str.charAt(0).toUpperCase() +
str
Expand Down Expand Up @@ -56,18 +65,6 @@ export const returnComponentFormat = (
);
};

// export function getDefaultsFromZodObject<Schema extends AnyZodObject>(
// schema: Schema,
// ) {
// return Object.fromEntries(
// Object.entries(schema.shape).map(([key, value]) => {
// if (value instanceof z.ZodDefault)
// return [key, value._def.defaultValue()];
// return [key, undefined];
// }),
// );
// }

export function getDefaultsFromZodObject<T extends z.ZodTypeAny>(
schema: z.AnyZodObject | z.ZodEffects<any>,
): z.infer<T> {
Expand Down

0 comments on commit 70d90ac

Please sign in to comment.