Skip to content

Commit

Permalink
feat(plugin-basic-ui): remove div wrapper in plugin-basic-ui
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyfromundefined committed Aug 14, 2024
1 parent 1fc97b4 commit bff6602
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 104 deletions.
24 changes: 0 additions & 24 deletions extensions/plugin-basic-ui/src/basicUIPlugin.css.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {
createGlobalTheme,
createGlobalThemeContract,
createTheme,
} from "@vanilla-extract/css";
import { recipe } from "@vanilla-extract/recipes";

const GLOBAL_VARS = {
backgroundColor: "background-color",
Expand Down Expand Up @@ -81,13 +79,6 @@ const cupertinoValues: GlobalVars = {
},
};

export const android = createTheme(globalVars, {
...androidValues,
});
export const cupertino = createTheme(globalVars, {
...cupertinoValues,
});

createGlobalTheme(
":root[data-stackflow-plugin-basic-ui-theme=cupertino]",
globalVars,
Expand All @@ -98,18 +89,3 @@ createGlobalTheme(
globalVars,
androidValues,
);

export const stackWrapper = recipe({
base: {},
variants: {
theme: {
android,
cupertino,
},
loading: {
true: {
pointerEvents: "none",
},
},
},
});
69 changes: 35 additions & 34 deletions extensions/plugin-basic-ui/src/basicUIPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { assignInlineVars } from "@vanilla-extract/dynamic";
import { createContext, useContext } from "react";

import * as css from "./basicUIPlugin.css";
import { RootStyles } from "./components/RootStyles";
import type { RecursivePartial } from "./utils";
import { compact, compactMap, isBrowser } from "./utils";
import { compactMap } from "./utils";

type BasicUIPluginOptions = RecursivePartial<css.GlobalVars> & {
theme: "android" | "cupertino";
Expand Down Expand Up @@ -50,46 +51,46 @@ export const basicUIPlugin: (
const _options =
typeof options === "function" ? options({ initialContext }) : options;

/**
* Assign CSS Variables
*/
const styles: { [key: string]: string | undefined } = assignInlineVars(
compactMap({
[css.globalVars.backgroundColor]: _options.backgroundColor,
[css.globalVars.dimBackgroundColor]: _options.dimBackgroundColor,
[css.globalVars.transitionDuration]: `${stack.transitionDuration}ms`,
[css.globalVars.computedTransitionDuration]:
stack.globalTransitionState === "loading"
? `${stack.transitionDuration}ms`
: "0ms",
[css.globalVars.appBar.borderColor]: _options.appBar?.borderColor,
[css.globalVars.appBar.borderSize]: _options.appBar?.borderSize,
[css.globalVars.appBar.height]: _options.appBar?.height,
[css.globalVars.appBar.iconColor]: _options.appBar?.iconColor,
[css.globalVars.appBar.textColor]: _options.appBar?.textColor,
[css.globalVars.appBar.minSafeAreaInsetTop]:
_options.appBar?.minSafeAreaInsetTop,
[css.globalVars.bottomSheet.borderRadius]:
_options.bottomSheet?.borderRadius,
[css.globalVars.modal.borderRadius]: _options.modal?.borderRadius,
}),
);

/**
* Prevent pointer events when transitioning
*/
styles["pointer-events"] =
stack.globalTransitionState === "loading" ? "none" : "auto";

return (
<GlobalOptionsProvider
value={{
...options,
theme: initialContext?.theme ?? _options.theme,
}}
>
<div
className={compact([
css.stackWrapper({
theme: initialContext?.theme ?? _options.theme,
loading: stack.globalTransitionState === "loading",
}),
_options.rootClassName,
]).join(" ")}
style={assignInlineVars(
compactMap({
[css.globalVars.backgroundColor]: _options.backgroundColor,
[css.globalVars.dimBackgroundColor]: _options.dimBackgroundColor,
[css.globalVars.transitionDuration]:
`${stack.transitionDuration}ms`,
[css.globalVars.computedTransitionDuration]:
stack.globalTransitionState === "loading"
? `${stack.transitionDuration}ms`
: "0ms",
[css.globalVars.appBar.borderColor]: _options.appBar?.borderColor,
[css.globalVars.appBar.borderSize]: _options.appBar?.borderSize,
[css.globalVars.appBar.height]: _options.appBar?.height,
[css.globalVars.appBar.iconColor]: _options.appBar?.iconColor,
[css.globalVars.appBar.textColor]: _options.appBar?.textColor,
[css.globalVars.appBar.minSafeAreaInsetTop]:
_options.appBar?.minSafeAreaInsetTop,
[css.globalVars.bottomSheet.borderRadius]:
_options.bottomSheet?.borderRadius,
[css.globalVars.modal.borderRadius]: _options.modal?.borderRadius,
}),
)}
>
{stack.render()}
</div>
<RootStyles theme={_options.theme} styles={styles} />
{stack.render()}
</GlobalOptionsProvider>
);
},
Expand Down
75 changes: 39 additions & 36 deletions extensions/plugin-basic-ui/src/components/AppBar.css.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { style } from "@vanilla-extract/css";
import { recipe } from "@vanilla-extract/recipes";

import { android, cupertino, globalVars } from "../basicUIPlugin.css";
import { globalVars } from "../basicUIPlugin.css";
import { f } from "../styles";
import {
background,
Expand Down Expand Up @@ -40,17 +40,18 @@ export const appBar = recipe({
zIndex: vars.zIndexes.appBar,
transition: transitions(appBarCommonTransition),
selectors: {
[`${cupertino} &`]: {
":root[data-stackflow-plugin-basic-ui-theme=cupertino] &": {
position: "absolute",
},
[`${cupertino} ${exitActive} &`]: {
transform: "translate3d(100%, 0, 0)",
transition: transitions({
...appBarCommonTransition,
transform: "0s",
}),
},
[`${android} &`]: {
[`:root[data-stackflow-plugin-basic-ui-theme=cupertino] ${exitActive} &`]:
{
transform: "translate3d(100%, 0, 0)",
transition: transitions({
...appBarCommonTransition,
transform: "0s",
}),
},
":root[data-stackflow-plugin-basic-ui-theme=android] &": {
opacity: 0,
transform: "translate3d(0, 10rem, 0)",
transition: transitions({
Expand All @@ -60,8 +61,8 @@ export const appBar = recipe({
}),
},
[`
${android} ${enterActive} &,
${android} ${enterDone} &
:root[data-stackflow-plugin-basic-ui-theme=android] ${enterActive} &,
:root[data-stackflow-plugin-basic-ui-theme=android] ${enterDone} &
`]: {
opacity: 1,
transform: "translate3d(0, 0, 0)",
Expand All @@ -78,7 +79,7 @@ export const appBar = recipe({
modalPresentationStyle: {
fullScreen: {
selectors: {
[`${cupertino} &`]: {
":root[data-stackflow-plugin-basic-ui-theme=cupertino] &": {
transform: "translate3d(0, 100vh, 0)",
transition: transitions({
...appBarCommonTransition,
Expand All @@ -87,36 +88,38 @@ export const appBar = recipe({
}),
},
[`
${cupertino} ${enterActive} &,
${cupertino} ${enterDone} &
:root[data-stackflow-plugin-basic-ui-theme=cupertino] ${enterActive} &,
:root[data-stackflow-plugin-basic-ui-theme=cupertino] ${enterDone} &
`]: {
transform: "translate3d(0, 0, 0)",
},
[`${cupertino} ${exitActive} &`]: {
transform: "translate3d(0, 100vh, 0)",
transition: transitions({
...appBarCommonTransition,
transform: vars.transitionDuration,
opacity: vars.transitionDuration,
}),
},
[`:root[data-stackflow-plugin-basic-ui-theme=cupertino] ${exitActive} &`]:
{
transform: "translate3d(0, 100vh, 0)",
transition: transitions({
...appBarCommonTransition,
transform: vars.transitionDuration,
opacity: vars.transitionDuration,
}),
},
},
},
},
activityEnterStyle: {
slideInLeft: {
selectors: {
[`${android} &`]: {
":root[data-stackflow-plugin-basic-ui-theme=android] &": {
opacity: 1,
transform: "translate3d(0, 0, 0)",
},
[`${android} ${exitActive} &`]: {
transform: "translate3d(100%, 0, 0)",
transition: transitions({
...appBarCommonTransition,
transform: "0s",
}),
},
[`:root[data-stackflow-plugin-basic-ui-theme=android] ${exitActive} &`]:
{
transform: "translate3d(100%, 0, 0)",
transition: transitions({
...appBarCommonTransition,
transform: "0s",
}),
},
},
},
},
Expand Down Expand Up @@ -189,7 +192,7 @@ export const centerMain = recipe({
color: globalVars.appBar.textColorTransitionDuration,
}),
selectors: {
[`${android} &`]: {
":root[data-stackflow-plugin-basic-ui-theme=android] &": {
width: "100%",
justifyContent: "flex-start",
paddingLeft: "1rem",
Expand All @@ -198,7 +201,7 @@ export const centerMain = recipe({
fontWeight: "bold",
boxSizing: "border-box",
},
[`${cupertino} &`]: {
":root[data-stackflow-plugin-basic-ui-theme=cupertino] &": {
position: "absolute",
display: "flex",
alignItems: "center",
Expand All @@ -221,7 +224,7 @@ export const centerMain = recipe({
hasLeft: {
true: {
selectors: {
[`${android} &`]: {
":root[data-stackflow-plugin-basic-ui-theme=android] &": {
paddingLeft: "0.375rem",
},
},
Expand All @@ -243,7 +246,7 @@ export const centerMainEdge = style([
display: "none",
width: vars.appBar.center.mainWidth,
selectors: {
[`${cupertino} &`]: {
":root[data-stackflow-plugin-basic-ui-theme=cupertino] &": {
display: "block",
},
},
Expand Down Expand Up @@ -273,7 +276,7 @@ export const right = style([
display: "none",
},
selectors: {
[`${android} &`]: {
":root[data-stackflow-plugin-basic-ui-theme=android] &": {
padding: "0 0.5rem 0 0",
},
},
Expand Down
20 changes: 10 additions & 10 deletions extensions/plugin-basic-ui/src/components/AppScreen.css.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createThemeContract, style } from "@vanilla-extract/css";
import { recipe } from "@vanilla-extract/recipes";

import { android, cupertino, globalVars } from "../basicUIPlugin.css";
import { globalVars } from "../basicUIPlugin.css";
import { f } from "../styles";

export const vars = createThemeContract({
Expand Down Expand Up @@ -59,7 +59,7 @@ export const dim = style([
opacity: 0,
zIndex: vars.zIndexes.dim,
selectors: {
[`${android} &`]: {
":root[data-stackflow-plugin-basic-ui-theme=android] &": {
height: "10rem",
background: `linear-gradient(${globalVars.dimBackgroundColor}, rgba(0, 0, 0, 0))`,
},
Expand Down Expand Up @@ -92,22 +92,22 @@ export const paper = recipe({
},
zIndex: vars.zIndexes.paper,
selectors: {
[`${cupertino} &`]: {
":root[data-stackflow-plugin-basic-ui-theme=cupertino] &": {
transform: "translate3d(100%, 0, 0)",
},
[`
${cupertino} ${enterActive} &,
${cupertino} ${enterDone} &
:root[data-stackflow-plugin-basic-ui-theme=cupertino] ${enterActive} &,
:root[data-stackflow-plugin-basic-ui-theme=cupertino] ${enterDone} &
`]: {
transform: "translate3d(0, 0, 0)",
},
[`${android} &`]: {
":root[data-stackflow-plugin-basic-ui-theme=android] &": {
opacity: 0,
transform: "translate3d(0, 10rem, 0)",
},
[`
${android} ${enterActive} &,
${android} ${enterDone} &
:root[data-stackflow-plugin-basic-ui-theme=android] ${enterActive} &,
:root[data-stackflow-plugin-basic-ui-theme=android] ${enterDone} &
`]: {
opacity: 1,
transform: "translate3d(0, 0, 0)",
Expand Down Expand Up @@ -146,7 +146,7 @@ export const paper = recipe({
modalPresentationStyle: {
fullScreen: {
selectors: {
[`${cupertino} &`]: {
":root[data-stackflow-plugin-basic-ui-theme=cupertino] &": {
transform: "translate3d(0, 100%, 0)",
},
},
Expand All @@ -155,7 +155,7 @@ export const paper = recipe({
activityEnterStyle: {
slideInLeft: {
selectors: {
[`${android} &`]: {
":root[data-stackflow-plugin-basic-ui-theme=android] &": {
transform: "translate3d(50%, 0, 0)",
},
},
Expand Down
27 changes: 27 additions & 0 deletions extensions/plugin-basic-ui/src/components/RootStyles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useLayoutEffect } from "react";

export type RootStylesProps = {
theme: "android" | "cupertino";
styles: {
[cssVarName: string]: string | undefined;
};
};
export function RootStyles({ theme, styles }: RootStylesProps) {
useLayoutEffect(() => {
const root = document.querySelector(":root") as HTMLHtmlElement;

if (!root) {
return;
}

for (const [key, value] of Object.entries(styles)) {
if (value) {
root.style.setProperty(key, value);
}
}

root.dataset.stackflowPluginBasicUiTheme = theme;
}, [styles, theme]);

return null;
}

0 comments on commit bff6602

Please sign in to comment.