Skip to content
This repository has been archived by the owner on Jul 3, 2023. It is now read-only.

Commit

Permalink
Add responsive utillity
Browse files Browse the repository at this point in the history
  • Loading branch information
naufalweise committed Jul 27, 2022
1 parent 51d079c commit aec20f8
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 38 deletions.
16 changes: 7 additions & 9 deletions components/typography/Body.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { VIEWPORTS } from "@constants";
import { WindowSize } from "@components/hooks";
import { ReactNode } from "react";
import { responsive } from "@utils";

type BodyPreset = "p1" | "p2" | "p3" | "b1" | "b2" | "b3";

Expand Down Expand Up @@ -44,13 +44,11 @@ export const BodyResponsive = ({
windowSize,
...props
}: BodyResponsiveProps) => {
let presetResponsive: BodyPreset;
if (windowSize.width >= VIEWPORTS.DESKTOP && presetDesktop) {
presetResponsive = presetDesktop;
} else if (windowSize.width >= VIEWPORTS.TABLET && presetTablet) {
presetResponsive = presetTablet;
} else {
presetResponsive = preset;
}
const presetResponsive = responsive<BodyPreset>(
windowSize,
preset,
presetTablet,
presetDesktop
);
return <Body preset={presetResponsive} {...props} />;
};
18 changes: 8 additions & 10 deletions components/typography/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ReactNode } from "react";
import { useWindowSize, WindowSize } from "@components/hooks/useWindowsSize";
import { VIEWPORTS } from "@constants";
import { WindowSize } from "@components/hooks/";
import { responsive } from "utils";

type HeaderPreset =
| "decorative"
Expand Down Expand Up @@ -65,13 +65,11 @@ export const HeaderResponsive = ({
windowSize,
...props
}: HeaderResponsiveProps) => {
let presetResponsive: HeaderPreset;
if (windowSize.width >= VIEWPORTS.DESKTOP && presetDesktop) {
presetResponsive = presetDesktop;
} else if (windowSize.width >= VIEWPORTS.TABLET && presetTablet) {
presetResponsive = presetTablet;
} else {
presetResponsive = preset;
}
const presetResponsive = responsive<HeaderPreset>(
windowSize,
preset,
presetTablet,
presetDesktop
);
return <Header preset={presetResponsive} {...props} />;
};
12 changes: 8 additions & 4 deletions pages/arutala.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import {
ArrowDownIcon,
} from "@heroicons/react/outline";
import { InputField, SelectField, FormLabel } from "@components/input-field";
import { Checkbox, Divider, FormControl, Stack } from "@chakra-ui/react";
import { Checkbox, FormControl, Stack } from "@chakra-ui/react";
import { ChevronUpIcon } from "@heroicons/react/outline";
import { responsive } from "@utils";

const Arutala: NextPage = () => {
const errorToast = Toast({
Expand Down Expand Up @@ -566,9 +567,12 @@ function TypographyResponsiveSection({
presetTablet="p2"
presetDesktop="p1"
>
Ukuran teks ini akan berganti sesuai dengan viewport. Lorem ipsum dolor
sit amet consectetur adipisicing elit. Doloremque iure officiis ex
laboriosam, eius illo? Ipsum quod obcaecati quidem maiores?
{responsive<string>(
windowSize,
"This is Mobile",
"This is Tablet",
"This is Desktop"
)}
</BodyResponsive>
</div>
);
Expand Down
65 changes: 50 additions & 15 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
Expand All @@ -16,19 +20,50 @@
"incremental": true,
"baseUrl": ".",
"paths": {
"@api/*": ["api/*"],
"@components": ["components"],
"@components/*": ["components/*"],
"@constants": ["constants"],
"@icons/*": ["public/assets/icons/*"],
"@images/*": ["public/assets/images/*"],
"@models": ["models"],
"@styles/*": ["styles/*"],
"@pages/*": ["pages/*"],
"@hooks": ["components/hooks"],
"@hooks/*": ["components/hooks/*"]
"@api/*": [
"api/*"
],
"@components": [
"components"
],
"@components/*": [
"components/*"
],
"@constants": [
"constants"
],
"@icons/*": [
"public/assets/icons/*"
],
"@images/*": [
"public/assets/images/*"
],
"@models": [
"models"
],
"@styles/*": [
"styles/*"
],
"@utils": [
"utils"
],
"@pages/*": [
"pages/*"
],
"@hooks": [
"components/hooks"
],
"@hooks/*": [
"components/hooks/*"
]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx"
],
"exclude": [
"node_modules"
]
}
1 change: 1 addition & 0 deletions utils/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./responsive";
20 changes: 20 additions & 0 deletions utils/responsive.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { WindowSize } from "@components/hooks";
import { VIEWPORTS } from "@constants";

/** Return responsive value according to viewport.*/
export function responsive<Type>(
windowSize: WindowSize,
mobile: Type,
tablet?: Type,
desktop?: Type
): Type {
let value: Type;
if (windowSize.width >= VIEWPORTS.DESKTOP && desktop) {
value = desktop;
} else if (windowSize.width >= VIEWPORTS.TABLET && tablet) {
value = tablet;
} else {
value = mobile;
}
return value;
}

0 comments on commit aec20f8

Please sign in to comment.