Skip to content

Commit 8fc5523

Browse files
committed
Add custom base font
1 parent 782dcaa commit 8fc5523

10 files changed

Lines changed: 532 additions & 204 deletions

File tree

apps/plugin/plugin-src/code.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const defaultPluginSettings: PluginSettings = {
3333
embedVectors: false,
3434
htmlGenerationMode: "html",
3535
tailwindGenerationMode: "jsx",
36+
baseFontSize: 16,
3637
};
3738

3839
// A helper type guard to ensure the key belongs to the PluginSettings type
@@ -175,7 +176,7 @@ const codegenMode = async () => {
175176
node,
176177
);
177178

178-
const nodeJson = await nodesToJSON([node]);
179+
const nodeJson = await nodesToJSON([node], userPluginSettings);
179180
const convertedSelection = await convertIntoNodes(nodeJson, null);
180181
console.log(
181182
"[DEBUG] codegen.generate - Converted selection:",

apps/plugin/ui-src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export default function App() {
124124
};
125125
const handlePreferencesChange = (
126126
key: keyof PluginSettings,
127-
value: boolean | string,
127+
value: boolean | string | number,
128128
) => {
129129
if (state.settings && state.settings[key] === value) {
130130
// do nothing

packages/backend/src/tailwind/builderImpl/tailwindSize.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,11 @@ const formatTailwindSizeValue = (
1212
prefix: string,
1313
settings?: TailwindSettings,
1414
): string => {
15-
// Try to find a matching Tailwind size class
16-
if (settings?.roundTailwindValues) {
17-
const tailwindSize = pxToLayoutSize(size);
15+
const tailwindSize = pxToLayoutSize(size);
1816

19-
// If we found a matching Tailwind class, use it
20-
if (!tailwindSize.startsWith("[")) {
21-
return `${prefix}-${tailwindSize}`;
22-
}
17+
// If we found a matching Tailwind class, use it
18+
if (!tailwindSize.startsWith("[")) {
19+
return `${prefix}-${tailwindSize}`;
2320
}
2421

2522
// No matching class or rounding disabled, use arbitrary value

packages/backend/src/tailwind/conversionTables.ts

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,17 @@ export const exactValue = (
4444
/**
4545
* convert pixel values to Tailwind attributes.
4646
* by default, Tailwind uses rem, while Figma uses px.
47-
* Therefore, a conversion is necessary. Rem = Pixel / 16.abs
47+
* Therefore, a conversion is necessary. Rem = Pixel / baseFontSize
4848
* Then, find in the corresponding table the closest value.
4949
*/
5050
const pxToRemToTailwind = (
5151
value: number,
5252
conversionMap: Record<number, string>,
5353
): string => {
5454
const keys = Object.keys(conversionMap).map((d) => +d);
55-
const remValue = value / 16;
55+
// Use the configured base font size or fall back to default 16px
56+
const baseFontSize = localTailwindSettings.baseFontSize || 16;
57+
const remValue = value / baseFontSize;
5658
const convertedValue = exactValue(remValue, keys);
5759

5860
if (convertedValue) {
@@ -76,6 +78,8 @@ const pxToTailwind = (
7678
const keys = Object.keys(conversionMap).map((d) => +d);
7779
const convertedValue = exactValue(value, keys);
7880

81+
console.log("convertedValue", convertedValue);
82+
7983
if (convertedValue) {
8084
return conversionMap[convertedValue];
8185
} else if (localTailwindSettings.roundTailwindValues) {
@@ -111,30 +115,16 @@ export const pxToBlur = (value: number): string | null => {
111115
};
112116

113117
export const pxToLayoutSize = (value: number): string => {
114-
// First check if it's a direct match to avoid rounding errors
115-
const exactValue = Object.keys(config.layoutSize)
116-
.map(Number)
117-
.find((size) => Math.abs(value - size) < 0.05);
118-
119-
if (exactValue !== undefined) {
120-
return (config.layoutSize as any)[exactValue];
121-
}
122-
123-
// If not an exact match but rounding is enabled, check for a close match
124-
if (localTailwindSettings.roundTailwindValues) {
125-
const thresholdValue = nearestValueWithThreshold(
126-
value,
127-
Object.keys(config.layoutSize).map(Number),
128-
15, // 15% threshold for layout sizes
129-
);
130-
131-
if (thresholdValue !== null) {
132-
return (config.layoutSize as any)[thresholdValue];
133-
}
134-
}
135-
136-
// No match found, return arbitrary value
137-
return `[${numberToFixedString(value)}px]`;
118+
// Scale the input value according to the base font size ratio
119+
const baseFontSize = localTailwindSettings.baseFontSize || 16;
120+
// If baseFontSize is different than 16, we need to adjust the pixel value
121+
// For example, with baseFontSize=14, 7px should match with the key for 8px (w-2)
122+
const scaledValue = (value * 16) / baseFontSize;
123+
124+
// Use pxToTailwind directly with the scaled value, since the keys in config.layoutSize
125+
// are likely in pixels based on a 16px base font size
126+
const result = pxToTailwind(scaledValue, config.layoutSize);
127+
return result !== null ? result : `[${numberToFixedString(value)}px]`;
138128
};
139129

140130
export const nearestOpacity = (nodeOpacity: number): number => {

packages/plugin-ui/src/PluginUI.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type PluginUIProps = {
3131
settings: PluginSettings | null;
3232
onPreferenceChanged: (
3333
key: keyof PluginSettings,
34-
value: boolean | string,
34+
value: boolean | string | number,
3535
) => void;
3636
colors: SolidColorConversion[];
3737
gradients: LinearGradientConversion[];
@@ -97,9 +97,9 @@ export const PluginUI = (props: PluginUIProps) => {
9797
{isEmpty === false && props.htmlPreview && (
9898
<Preview htmlPreview={props.htmlPreview} />
9999
)}
100-
100+
101101
{warnings.length > 0 && <WarningsPanel warnings={warnings} />}
102-
102+
103103
<CodePanel
104104
code={props.code}
105105
selectedFramework={props.selectedFramework}

packages/plugin-ui/src/components/CodePanel.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import EmptyState from "./EmptyState";
1212
import SettingsGroup from "./SettingsGroup";
1313
import CustomPrefixInput from "./CustomPrefixInput";
1414
import FrameworkTabs from "./FrameworkTabs";
15+
import { TailwindSettings } from "./tailwindSettings";
1516

1617
interface CodePanelProps {
1718
code: string;
@@ -21,7 +22,7 @@ interface CodePanelProps {
2122
selectPreferenceOptions: SelectPreferenceOptions[];
2223
onPreferenceChanged: (
2324
key: keyof PluginSettings,
24-
value: boolean | string,
25+
value: boolean | string | number,
2526
) => void;
2627
}
2728

@@ -202,9 +203,9 @@ const CodePanel = (props: CodePanelProps) => {
202203
onPreferenceChanged={onPreferenceChanged}
203204
>
204205
{selectedFramework === "Tailwind" && (
205-
<CustomPrefixInput
206-
initialValue={settings?.customTailwindPrefix || ""}
207-
onValueChange={handleCustomPrefixChange}
206+
<TailwindSettings
207+
settings={settings}
208+
onPreferenceChanged={onPreferenceChanged}
208209
/>
209210
)}
210211
</SettingsGroup>

0 commit comments

Comments
 (0)