Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
shivankacker committed Nov 30, 2023
1 parent 0d7d0c5 commit f9eca94
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kacker/ui",
"version": "1.1.1",
"version": "1.2.0",
"main": "./dist/cjs",
"module": "./dist/esm",
"types": "./dist/cjs/index.d.ts",
Expand Down
Binary file removed src/assets/fonts/manrope-variable.ttf
Binary file not shown.
1 change: 1 addition & 0 deletions src/css/page.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
opacity: 1;
visibility: visible;
transform: translateX(0);
transition: 0.1s ease-in-out;
}
1 change: 0 additions & 1 deletion src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
@tailwind utilities;

body {
font-family: var(--kui-font);
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
-webkit-tap-highlight-color: transparent;
}
14 changes: 11 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import './index.css';
import './css/fallback.css'
import './css/page.css'
import './assets/fonts/manrope-variable.ttf'
import Input from './interactive/Input';
import ThemeProvider from './providers/ThemeProvider';
import { InputProps } from './interactive/Input';
Expand Down Expand Up @@ -37,6 +36,10 @@ import Drawer from './layout/Drawer';
import { DrawerProps, WarningDrawer, WarningDrawerProps } from './layout/Drawer';
import Button from './interactive/buttons/Button';
import { ButtonProps } from './interactive/buttons/Button';
import { RawPage, RawPageProps } from './layout/Page';
import Router from './providers/RouterProvider'
import { RouterProps } from './providers/RouterProvider';
import * as raviger from 'raviger';
export {
Input,
ThemeProvider,
Expand All @@ -56,7 +59,10 @@ export {
Menu,
Drawer,
Button,
WarningDrawer
WarningDrawer,
RawPage,
Router,
raviger
};
export type {
InputProps,
Expand All @@ -78,5 +84,7 @@ export type {
MenuOption,
DrawerProps,
ButtonProps,
WarningDrawerProps
WarningDrawerProps,
RawPageProps,
RouterProps
};
21 changes: 18 additions & 3 deletions src/layout/Activity.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
import { twMerge } from "tailwind-merge";
import { RouteTree } from "../providers/RouterProvider";
import { useEffect } from "react";

export type ActivityProps = {
activityIndex: number;
className?: string;
children: React.ReactNode;
routeTree: RouteTree[];
setRouteTree: React.Dispatch<React.SetStateAction<RouteTree[]>>;
}

export default function Activity(props: ActivityProps) {

const { activityIndex, className, children } = props;
const { activityIndex, className, routeTree, setRouteTree } = props;

useEffect(() => {
setRouteTree((prev) => [
...prev.slice(0, activityIndex),
{
...prev[activityIndex],
status: "ready",
},
...prev.slice(activityIndex + 1),
]);
}, []);

return (
<div
style={{ zIndex: 100 + activityIndex }}
className={twMerge("w-screen-handler-screen h-screen w-screen fixed inset-y-0", className)}
data-route-index={activityIndex}
>
{children}
{routeTree[activityIndex].node}
</div>
)
}
15 changes: 15 additions & 0 deletions src/layout/Page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { twMerge } from "tailwind-merge";
import PageHeader, { PageHeaderProps } from "./PrimaryPageHeader";

export type RawPageProps = {
children: React.ReactNode;
className?: string;
}

export type PageProps = {
children: React.ReactNode;
type?: "sticky" | "fixed";
Expand Down Expand Up @@ -37,3 +42,13 @@ export default function Page(props: PageProps) {
</div>
);
}

export function RawPage(props: RawPageProps) {

const { className, children } = props;
return (
<div className={twMerge("h-screen bg-primary text-primaryFont pt-[var(--status-bar-height,0)] pb-[var(--safe-area-inset-bottom)]", className)}>
{children}
</div>
)
}
130 changes: 130 additions & 0 deletions src/providers/RouterProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import { Routes, usePath, useRoutes } from "raviger";
import AppContainer, { AppContainerProps } from "../layout/AppContainer";
import { RawPage } from "../layout/Page";
import React, { useEffect, useState } from "react";
import Activity from "../layout/Activity";

export type RouterProps = {
routes: Routes<string>
notFound?: React.ReactNode,
routeTree: RouteTree[],
setRouteTree: React.Dispatch<React.SetStateAction<RouteTree[]>>,
} & Omit<AppContainerProps, "currentPath" | "children" | "routeResult">

export type RouteTree = {
node: React.ReactNode;
path: string;
status?: "ready" | "destroying";
};

export default function Router(props: RouterProps) {
const path = usePath();
const { routes, exclusivePaths, baseRoutes, notFound, setRouteTree, routeTree } = props;
const routeResult = useRoutes(routes) || notFound || <RawPage>Not Found</RawPage>;
const [direction, setDirection] = useState<"backward" | "forward" | null>(
null
);

const popstateListener = (e: PopStateEvent) => {
if (e.state === undefined) {
setDirection("backward");
} else if (e.state === null) {
setDirection("forward");
}
};

useEffect(() => {
window.addEventListener("popstate", popstateListener);
if (
!baseRoutes.map(route => route.href).includes(path as string) &&
!exclusivePaths.includes(path as `/${string}`)
) {
setRouteTree([
...routeTree,
{
node: routeResult,
path: path as string,
},
]);
}
return () => {
window.removeEventListener("popstate", popstateListener);
};
}, []);

useEffect(() => {
if (!direction) return;
const newPath = path;
const lastPath = routeTree[routeTree.length - 1]?.path;
if (
direction === "backward" &&
!baseRoutes.map(route => route.href).includes(lastPath) &&
!exclusivePaths.includes(lastPath as `/${string}`)
) {
setRouteTree([
...routeTree.slice(0, -1),
{
...routeTree[routeTree.length - 1],
status: "destroying",
},
]);
} else if (
direction === "forward" &&
!baseRoutes.map(route => route.href).includes(newPath as string) &&
!exclusivePaths.includes(newPath as `/${string}`)
) {
setRouteTree([
...routeTree,
{
node: routeResult,
path: path as string,
},
]);
}
setDirection(null);
}, [direction]);

useEffect(() => {
routeTree.forEach((route, i) => {
if (route.status === "destroying") {
const el = document.querySelector(`[data-route-index="${i}"]`);
if (el) {
el.classList.remove(
"__kui__screen__handler__screen__ready"
);
}
setTimeout(() => {
setRouteTree((prev) =>
prev.filter((_, index) => index !== i)
);
}, 150);
} else if (route.status === "ready") {
const el = document.querySelector(`[data-route-index="${i}"]`);
if (el) {
el.classList.add("__kui__screen__handler__screen__ready");
}
}
});
console.log(routeTree);
}, [routeTree]);

return (
<>
<AppContainer
exclusivePaths={exclusivePaths}
routeResult={routeResult}
baseRoutes={baseRoutes}
currentPath={path || ("/" as any)}
>
{routeTree.map((_, i) => (
<Activity
key={i}
activityIndex={i}
routeTree={routeTree}
setRouteTree={setRouteTree}
/>
))}
</AppContainer>
</>
);
}
3 changes: 0 additions & 3 deletions src/providers/ThemeProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useEffect } from "react";
import { Themes, setTheme, useThemeDetector } from "../utils/themes";
import { Theme } from "../types/themes";
import Manrope from '../assets/fonts/manrope-variable.ttf'

export type ThemeProviderProps = {
children: React.ReactNode;
Expand Down Expand Up @@ -39,8 +38,6 @@ export default function ThemeProvider(props: ThemeProviderProps) {

useEffect(() => {

document.documentElement.style.setProperty(`--kui-font`, `url(${Manrope})`);

if (accent)
Object.keys(accent).forEach((key) => {
document.documentElement.style.setProperty(`--kui-accent${key}`, accent[key]);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/themes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,4 @@ export const useThemeDetector = () => {
return () => darkThemeMq.removeListener(mqListener);
}, []);
return isDarkTheme;
};
};
1 change: 1 addition & 0 deletions tailwind.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/** @type {import('tailwindcss').Config} */
module.exports = {
important: true,
content: [
"./index.html",
"./src/**/*.{js,ts,jsx,tsx}",
Expand Down

0 comments on commit f9eca94

Please sign in to comment.