Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add components, clean styles #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions app/Components/Heading.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Components;

use App\Component;

class Heading extends Component
{
protected $heading;
protected $isH1;
protected $headingSize;

public function __construct($heading, $isH1, $headingSize)
{
$this->heading = $heading;
$this->isH1 = $isH1;
$this->headingSize = $headingSize;
}

public function render()
{
return $this->renderView('heading');
}
}
44 changes: 44 additions & 0 deletions app/Components/SectionHeader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\View\Components;

use App\Component;

class SectionHeader extends Component
{
public $showEyebrow;
public $eyebrow;
public $showHeading;
public $heading;
public $showText;
public $text;
public $isH1;
public $textAlign;

public function __construct(
$data = [],
$showEyebrow = null,
$eyebrow = null,
$showHeading = null,
$heading = null,
$showText = null,
$text = null,
$isH1 = null,
$textAlign = null
) {

$this->showEyebrow = $showEyebrow ?? $data['showEyebrow'] ?? false;
$this->eyebrow = $eyebrow ?? $data['eyebrow'] ?? '';
$this->showHeading = $showHeading ?? $data['showHeading'] ?? false;
$this->heading = $heading ?? $data['heading'] ?? [];
$this->showText = $showText ?? $data['showText'] ?? false;
$this->text = $text ?? $data['text'] ?? '';
$this->isH1 = $isH1 ?? $data['isH1'] ?? false;
$this->textAlign = $textAlign ?? $data['textAlign'] ?? 'left';
}

public function render()
{
return $this->renderView('section-header');
}
}
Empty file added includes/template-tags.php
Empty file.
253 changes: 72 additions & 181 deletions resources/scripts/editor/components/button/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@ import {
URLInputButton,
useBlockProps,
} from "@wordpress/block-editor";
import { PanelBody, SelectControl, ToggleControl } from "@wordpress/components";
import { SelectControl, ToggleControl } from "@wordpress/components";
import { __ } from "@wordpress/i18n";
import clsx from "clsx";
import {
ComponentSidebar,
PopoverPlacement,
} from "scripts/editor/utils/components/sidebar";
import {
createDefaultAttributes,
GetBlockAttributeValues,
GetSetAttributesFunction,
} from "scripts/editor/utils/type-mapping";
import clsx from "clsx";
import PlusSign from "images/plusSign";
import ArrowRight from "images/arrowRight";
import AppStore from "images/app-store.svg";
import PlayStore from "images/google-play.svg";
import AppGallery from "images/app-gallery.svg";

/* Component attributes */
const normalButtonTypes = ["primary", "secondary", "accent"];
const storeButtonTypes = ["appStore", "playStore", "appGallery"];
export const attributes = {
text: {
type: "string",
Expand All @@ -29,68 +26,42 @@ export const attributes = {
type: "string",
default: "",
},
showIcon: {
type: "boolean",
default: false,
},
iconOnRight: {
type: "boolean",
default: false,
},
isLoading: {
type: "boolean",
default: false,
},
size: {
type: "string",
enum: ["s", "m"],
default: "m",
},
type: {
type: "string",
enum: [...normalButtonTypes, "icon", ...storeButtonTypes],
default: "primary",
},
openInNewTab: {
type: "boolean",
default: false,
},
linkType: {
type: "string",
enum: ["custom", "post"],
default: "custom",
},
linkId: {
type: "number",
default: 0,
},
} as const;

/* Component types */
export type BlockAttributeValues = GetBlockAttributeValues<typeof attributes>;
type SetAttributesFunction = GetSetAttributesFunction<typeof attributes>;
export const defaultAttributes: BlockAttributeValues =
createDefaultAttributes(attributes);

/* Component styles */
const styles = {
button:
"justify-center focus:outline-none focus:ring-4 focus:ring-blue-600 disabled:text-neutral-300 disabled:bg-neutral-100 disabled:border-neutral-100",
text: "text-xs font-serif font-bold",
iconSize: {
s: "px-1 py-1 w-8 h-8",
m: "px-2 py-2 w-11 h-11",
},
shared: {
store: "px-4 py-3 rounded-xl hover:shadow-lg",
regular: "border w-full lg:w-fit px-5 rounded-full",
icon: "border rounded-full",
},
"py-2 px-4 rounded-lg shadow-md transition duration-300 ease-in-out w-fit border border-black",
text: "text-sm font-semibold",
type: {
primary:
"bg-black border-black hover:bg-neutral-700 hover:border-neutral-700 active:bg-neutral-600 active:border-neutral-600 text-white",
secondary:
"bg-white border-neutral-300 hover:bg-neutral-100 hover:border-neutral-400 active:bg-neutral-200 active:border-neutral-400 text-black",
accent:
"bg-green-400 border-green-400 hover:bg-green-300 hover:border-green-300 active:bg-green-200 active:border-green-200 text-black disabled:bg-white",
icon: "text-black dark:text-white bg-white dark:bg-neutral-700 border-neutral-300 dark:border-neutral-700 hover:bg-neutral-100 hover:border-neutral-400 dark:hover:bg-neutral-600 dark:hover:border-neutral-600 active:bg-neutral-200 active:border-neutral-500 dark:active:bg-neutral-500 disabled:border-neutral-100 dark:disabled:bg-neutral-700 dark:disabled:border-neutral-700",
appStore: "bg-black",
playStore: "bg-white border border-neutral-200",
appGallery: "bg-white border border-neutral-200",
primary: "bg-white text-black hover:bg-gray-200",
secondary: "bg-black text-white hover:bg-gray-800",
},
};

/* Component types */
export type BlockAttributeValues = GetBlockAttributeValues<typeof attributes>;
type SetAttributesFunction = GetSetAttributesFunction<typeof attributes>;
export const defaultAttributes: BlockAttributeValues =
createDefaultAttributes(attributes);

/* Component edit */
export const Edit = ({
attributes,
Expand All @@ -99,101 +70,47 @@ export const Edit = ({
attributes: BlockAttributeValues;
setAttributes: SetAttributesFunction;
}) => {
const { text, href, type, size, iconOnRight, openInNewTab } = attributes;
const { text, href, type, openInNewTab } = attributes;
const blockProps = useBlockProps();
const buttonSizeClass = type === "icon" ? styles?.iconSize?.[size] : "py-3";

const typeClass = clsx({
[styles?.type?.[type]]: true,
[styles?.shared?.store]: storeButtonTypes?.includes(type),
[styles?.shared?.regular]: normalButtonTypes?.includes(type),
[styles?.shared?.icon]: type === "icon",
});
const onChangeLink = (newURL, post) => {
if (post && post.kind === "post-type" && post.id) {
setAttributes({
...attributes,
linkType: "post",
linkId: post.id,
href: newURL,
});
} else {
setAttributes({
...attributes,
linkType: "custom",
linkId: 0,
href: newURL,
});
}
};

return (
<>
<div {...blockProps} className={clsx(type, "flex flex-col")}>
{!storeButtonTypes.includes(type) && (
<div className="flex flex-col bg-white w-fit">
<URLInputButton
url={href}
onChange={(href) => setAttributes({ ...attributes, href })}
/>
<ToggleControl
label={__("Open in new tab")}
checked={openInNewTab}
onChange={(openInNewTab) =>
setAttributes({ ...attributes, openInNewTab })
}
/>
</div>
)}
<div
className={clsx(
"flex gap-1",
styles?.text,
styles?.button,
buttonSizeClass,
typeClass
)}
>
{(() => {
switch (type) {
case "icon":
return <PlusSign className="fill-current" />;
case "appStore":
return (
<img
className="object-none"
src={AppStore}
alt="App Store logo"
aria-hidden="true"
/>
);
case "playStore":
return (
<img
className="object-none"
src={PlayStore}
alt="Google Play logo"
aria-hidden="true"
/>
);
case "appGallery":
return (
<img
className="object-none"
src={AppGallery}
alt="App Gallery logo"
aria-hidden="true"
/>
);

default:
return (
<>
{attributes.showIcon && (
<div
className={clsx(
iconOnRight ? "order-last" : "order-first"
)}
>
<ArrowRight className="object-none" />
</div>
)}
<RichText
tagName="p"
placeholder={__("Button")}
value={text}
onChange={(text) =>
setAttributes({ ...attributes, text })
}
/>
</>
);
<div {...blockProps} className={type}>
<div className="flex flex-col bg-white w-fit">
<URLInputButton url={href} onChange={onChangeLink} />
<ToggleControl
label={__("Open in new tab")}
checked={openInNewTab}
onChange={(openInNewTab) =>
setAttributes({ ...attributes, openInNewTab })
}
})()}
/>
</div>
<RichText
tagName="p"
placeholder={__("Button")}
value={text}
className={clsx(styles.button, styles.text, styles.type[type])}
onChange={(text) => setAttributes({ ...attributes, text })}
/>
</div>
</>
);
Expand All @@ -205,60 +122,34 @@ export const Sidebar = ({
setAttributes,
title,
children,
popoverSidebar = false,
popoverPlacement = "overlay",
}: {
attributes: BlockAttributeValues;
setAttributes: SetAttributesFunction;
title?: string;
children?: React.ReactNode;
popoverSidebar?: boolean;
popoverPlacement?: PopoverPlacement;
}) => {
const { type, showIcon, size, iconOnRight } = attributes;

const { type } = attributes;
const sidebarTitle = __(title ?? "Button Settings");
return (
<PanelBody title={__(title ?? "Button Settings")} initialOpen>
<ComponentSidebar
popoverSidebar={popoverSidebar}
title={sidebarTitle}
placement={popoverPlacement}
>
<SelectControl
label="Type"
value={type}
options={[
{ label: "Primary", value: "primary" },
{ label: "Secondary", value: "secondary" },
{ label: "Accent", value: "accent" },
{ label: "Icon", value: "icon" },
{ label: "App Store", value: "appStore" },
{ label: "Play Store", value: "playStore" },
{ label: "App Gallery", value: "appGallery" },
]}
onChange={(value) => setAttributes({ ...attributes, type: value })}
onChange={(type) => setAttributes({ ...attributes, type })}
/>
{type === "icon" && (
<SelectControl
label="Size"
value={size}
options={[
{ label: "Small", value: "s" },
{ label: "Medium", value: "m" },
]}
onChange={(value) => setAttributes({ ...attributes, size: value })}
/>
)}
{normalButtonTypes?.includes(type) && (
<>
<ToggleControl
label={__("Show Icon")}
checked={showIcon}
onChange={(showIcon) => setAttributes({ ...attributes, showIcon })}
/>
{showIcon && (
<ToggleControl
label={__("Icon on Right")}
checked={iconOnRight}
onChange={(iconOnRight) =>
setAttributes({ ...attributes, iconOnRight })
}
/>
)}
</>
)}
{children}
</PanelBody>
</ComponentSidebar>
);
};
Loading