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

feat: font family selection #154

Merged
merged 5 commits into from
Nov 15, 2024
Merged
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
30 changes: 14 additions & 16 deletions src/components/puck/BodyText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
YextEntityField,
YextEntityFieldSelector,
NumberFieldWithDefaultOption,
getFontWeightOverrideOptions,
} from "../../index.ts";

export interface BodyTextProps extends BodyProps {
Expand Down Expand Up @@ -44,22 +45,6 @@ const bodyTextFields: Fields<BodyTextProps> = {
label: "Font Size",
defaultCustomValue: 12,
}),
fontWeight: {
label: "Font Weight",
type: "select",
options: [
{ label: "Default", value: "default" },
{ label: "Thin", value: "thin" },
{ label: "Extra Light", value: "extralight" },
{ label: "Light", value: "light" },
{ label: "Normal", value: "normal" },
{ label: "Medium", value: "medium" },
{ label: "Semibold", value: "semibold" },
{ label: "Bold", value: "bold" },
{ label: "Extrabold", value: "extrabold" },
{ label: "Black", value: "black" },
],
},
color: {
label: "Color",
type: "select",
Expand Down Expand Up @@ -98,6 +83,19 @@ export const BodyTextComponent: ComponentConfig<BodyTextProps> = {
color: "default",
textTransform: "none",
},
resolveFields: async () => {
const fontWeightOptions = await getFontWeightOverrideOptions({
fontCssVariable: "--fontFamily-body-fontFamily",
});
return {
...bodyTextFields,
fontWeight: {
label: "Font Weight",
type: "select",
options: fontWeightOptions,
},
};
},
render: (props) => <BodyText {...props} />,
};

Expand Down
30 changes: 14 additions & 16 deletions src/components/puck/HeadingText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
YextEntityField,
YextEntityFieldSelector,
NumberFieldWithDefaultOption,
getFontWeightOverrideOptions,
} from "../../index.js";

export interface HeadingTextProps extends HeadingProps {
Expand Down Expand Up @@ -50,22 +51,6 @@ const headingTextFields: Fields<HeadingTextProps> = {
label: "Font Size",
defaultCustomValue: 24,
}),
weight: {
label: "Font Weight",
type: "select",
options: [
{ label: "Default", value: "default" },
{ label: "Thin", value: "thin" },
{ label: "Extra Light", value: "extralight" },
{ label: "Light", value: "light" },
{ label: "Normal", value: "normal" },
{ label: "Medium", value: "medium" },
{ label: "Semibold", value: "semibold" },
{ label: "Bold", value: "bold" },
{ label: "Extrabold", value: "extrabold" },
{ label: "Black", value: "black" },
],
},
color: {
label: "Font Color",
type: "select",
Expand Down Expand Up @@ -107,6 +92,19 @@ export const HeadingTextComponent: ComponentConfig<HeadingTextProps> = {
color: "default",
transform: "none",
},
resolveFields: async (data) => {
const fontWeightOptions = await getFontWeightOverrideOptions({
fontCssVariable: `--fontFamily-heading${data.props.level}-fontFamily`,
});
return {
...headingTextFields,
weight: {
label: "Font Weight",
type: "select",
options: fontWeightOptions,
},
};
},
render: (props) => <HeadingText {...props} />,
};

Expand Down
20 changes: 10 additions & 10 deletions src/components/puck/atoms/body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import { clsx } from "clsx";
import { NumberOrDefault } from "../../editor/NumberOrDefaultField.tsx";

// Define the variants for the body component
const bodyVariants = cva("components text-body-fontSize", {
const bodyVariants = cva("components text-body-fontSize font-body-fontFamily", {
variants: {
fontWeight: {
default: "font-body-fontWeight",
thin: "font-thin",
extralight: "font-extralight",
light: "font-light",
normal: "font-normal",
medium: "font-medium",
semibold: "font-semibold",
bold: "font-bold",
extrabold: "font-extrabold",
black: "font-black",
"100": "font-thin",
"200": "font-extralight",
"300": "font-light",
"400": "font-normal",
"500": "font-medium",
"600": "font-semibold",
"700": "font-bold",
"800": "font-extrabold",
"900": "font-black",
},
color: {
default: "text-body-color",
Expand Down
30 changes: 15 additions & 15 deletions src/components/puck/atoms/heading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ const headingVariants = cva("components", {
variants: {
weight: {
default: "",
thin: "!font-thin",
extralight: "!font-extralight",
light: "!font-light",
normal: "!font-normal",
medium: "!font-medium",
semibold: "!font-semibold",
bold: "!font-bold",
extrabold: "!font-extrabold",
black: "!font-black",
"100": "!font-thin",
"200": "!font-extralight",
"300": "!font-light",
"400": "!font-normal",
"500": "!font-medium",
"600": "!font-semibold",
"700": "!font-bold",
"800": "!font-extrabold",
"900": "!font-black",
},
color: {
default: "",
Expand All @@ -32,12 +32,12 @@ const headingVariants = cva("components", {
capitalize: "capitalize",
},
level: {
1: "font-heading1-fontWeight text-heading1-fontSize text-heading1-color",
2: "font-heading2-fontWeight text-heading2-fontSize text-heading2-color",
3: "font-heading3-fontWeight text-heading3-fontSize text-heading3-color",
4: "font-heading4-fontWeight text-heading4-fontSize text-heading4-color",
5: "font-heading5-fontWeight text-heading5-fontSize text-heading5-color",
6: "font-heading6-fontWeight text-heading6-fontSize text-heading6-color",
1: "font-heading1-fontWeight text-heading1-fontSize text-heading1-color font-heading1-fontFamily",
2: "font-heading2-fontWeight text-heading2-fontSize text-heading2-color font-heading2-fontFamily",
3: "font-heading3-fontWeight text-heading3-fontSize text-heading3-color font-heading3-fontFamily",
4: "font-heading4-fontWeight text-heading4-fontSize text-heading4-color font-heading4-fontFamily",
5: "font-heading5-fontWeight text-heading5-fontSize text-heading5-color font-heading5-fontFamily",
6: "font-heading6-fontWeight text-heading6-fontSize text-heading6-color font-heading6-fontFamily",
},
},
defaultVariants: {
Expand Down
67 changes: 67 additions & 0 deletions src/internal/puck/components/FontSelector.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import React, { useState } from "react";
import { FieldLabel } from "@measured/puck";
import { StyleSelectOption } from "../../../utils/themeResolver.ts";
import "../ui/puck.css";
import { ChevronDown } from "lucide-react";

type FontSelectorProps = {
label: string;
options: StyleSelectOption[];
value: string;
onChange: (value: any) => void;
};

export const FontSelector = ({
label,
onChange,
value,
options,
}: FontSelectorProps) => {
const [isOpen, setIsOpen] = useState(false);

const toggleDropdown = () => {
setIsOpen(!isOpen);
};

const handleFontChange = (font: string) => {
onChange(font);
setIsOpen(false);
};

const selectedFontLabel =
options.find((option) => option.value === value)?.label ?? "Choose a font";

return (
<FieldLabel label={label} icon={<ChevronDown size={16} />}>
<div style={{ position: "relative" }}>
<button
onClick={toggleDropdown}
className="font-select"
style={{ fontFamily: value }}
>
{selectedFontLabel}
</button>

{isOpen && (
<div className="font-select-dropdown">
{options.map((option) => (
<button
key={option.value}
value={option.value}
onClick={() => handleFontChange(option.value)}
style={{
fontFamily: option.value,
backgroundColor:
option.value === value ? "var(--puck-color-grey-09)" : "",
}}
className="font-select-option"
>
{option.label}
</button>
))}
</div>
)}
</div>
</FieldLabel>
);
};
53 changes: 53 additions & 0 deletions src/internal/puck/ui/puck.css
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,56 @@
padding: 0;
border: none !important;
}

.font-select {
text-align: left;
width: 100%;
padding: 12px 15px;
font-size: 14px;
cursor: pointer;
background-color: var(--puck-color-white);
border: 1px solid var(--puck-color-grey-09);
border-radius: 4px;
transition: border-color 50ms ease-in;
background: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='100' height='100' fill='%23c3c3c3'><polygon points='0,0 100,0 50,50'/></svg>")
no-repeat;
background-size: 12px;
background-position: calc(100% - 12px) calc(50% + 3px);
background-repeat: no-repeat;
}

.font-select:hover,
.font-select:focus {
background-color: var(--puck-color-azure-12);
border-color: var(--puck-color-grey-05);
transition: none;
}

.font-select-dropdown {
position: absolute;
z-index: 1;
background-color: var(--puck-color-white);
border: 1px solid var(--puck-color-grey-09);
border-radius: 4px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
max-height: 200px;
overflow-y: auto;
}

.font-select-option {
text-align: left;
width: 100%;
padding: 8px 12px;
background-color: transparent;
border: none;
cursor: pointer;
font-size: 14px;
}

.font-select-option:hover {
background-color: var(--puck-color-grey-09);
}

.font-select-option:active {
background-color: var(--puck-color-grey-09);
}
37 changes: 30 additions & 7 deletions src/internal/utils/constructThemePuckFields.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe("constructThemePuckFields", () => {
},
family: {
label: "Font Family",
type: "select",
type: "custom",
},
},
});
Expand All @@ -114,7 +114,7 @@ describe("convertStyleToPuckField", () => {
default: 0,
};

const result = convertStyleToPuckField(numberStyle);
const result = convertStyleToPuckField(numberStyle, numberStyle.plugin);

expect(result).toEqual({
label: "Font Size",
Expand All @@ -124,6 +124,26 @@ describe("convertStyleToPuckField", () => {

it("converts select style to select field", () => {
const selectStyle: Style = {
label: "Underline",
type: "select",
plugin: "fontStyle",
default: "none",
options: [
{ label: "Underline", value: "underline" },
{ label: "None", value: "" },
],
};
const result = convertStyleToPuckField(selectStyle, selectStyle.plugin);

expect(result).toEqual({
label: "Underline",
type: "select",
options: selectStyle.options,
});
});

it("converts fontFamily style to font select field", () => {
const fontFamilyStyle: Style = {
label: "Font Family",
type: "select",
plugin: "fontFamily",
Expand All @@ -134,12 +154,15 @@ describe("convertStyleToPuckField", () => {
],
};

const result = convertStyleToPuckField(selectStyle);
const result = convertStyleToPuckField(
fontFamilyStyle,
fontFamilyStyle.plugin
);

expect(result).toEqual({
expect(result).toMatchObject({
label: "Font Family",
type: "select",
options: selectStyle.options,
type: "custom",
options: fontFamilyStyle.options,
});
});

Expand All @@ -151,7 +174,7 @@ describe("convertStyleToPuckField", () => {
plugin: "colors",
};

const result = convertStyleToPuckField(colorStyle);
const result = convertStyleToPuckField(colorStyle, colorStyle.plugin);

expect(result).toMatchObject({
label: "Main Color",
Expand Down
Loading