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

fix fluid node and accessible clamp node #528

Merged
merged 1 commit into from
Nov 20, 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
5 changes: 5 additions & 0 deletions .changeset/curly-rockets-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tokens-studio/graph-engine": patch
---

Fixing the issue of min and max size being mixed up in the fluid node, this wasn't resolving before.
5 changes: 5 additions & 0 deletions .changeset/twenty-cherries-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tokens-studio/graph-engine": patch
---

Fix issue on accessible clamp node that would break on mixed up min and max values.
22 changes: 18 additions & 4 deletions packages/graph-engine/src/nodes/css/accessibleClamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,28 @@ export default class NodeDefinition extends Node {
precision
} = this.getAllInputs();

// Get the actual min and max values regardless of input order
const actualMinSize = Math.min(minSize, maxSize);
const actualMaxSize = Math.max(minSize, maxSize);
const actualMinViewport = Math.min(minViewport, maxViewport);
const actualMaxViewport = Math.max(minViewport, maxViewport);

// Handle equal viewport case
if (actualMinViewport === actualMaxViewport) {
const minSizeRem = actualMinSize / baseFontSize;
this.outputs.value.set(`${Number(minSizeRem.toFixed(precision))}rem`);
return;
}

// Convert sizes to rem
const minSizeRem = minSize / baseFontSize;
const maxSizeRem = maxSize / baseFontSize;
const minSizeRem = actualMinSize / baseFontSize;
const maxSizeRem = actualMaxSize / baseFontSize;

// Calculate the preferred value parameters
const slope = (maxSize - minSize) / (maxViewport - minViewport);
const slope =
(actualMaxSize - actualMinSize) / (actualMaxViewport - actualMinViewport);
const viewportValue = slope * 100; // Convert to vw units
const intersect = minSize - slope * minViewport;
const intersect = actualMinSize - slope * actualMinViewport;
const relativeValue = intersect / baseFontSize; // Convert to rem

// Format the values with specified precision
Expand Down
67 changes: 56 additions & 11 deletions packages/graph-engine/src/nodes/math/fluid.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { INodeDefinition, ToInput, ToOutput } from '../../index.js';
import { Node } from '../../programmatic/node.js';
import { NumberSchema } from '../../schemas/index.js';
import { setToPrecision } from '../../utils/precision.js';

export default class NodeDefinition extends Node {
static title = 'Fluid';
Expand All @@ -14,6 +15,7 @@ export default class NodeDefinition extends Node {
minViewport: number;
maxViewport: number;
viewport: number;
precision: number;
}>;
declare outputs: ToOutput<{
value: number;
Expand All @@ -22,35 +24,78 @@ export default class NodeDefinition extends Node {
constructor(props: INodeDefinition) {
super(props);
this.addInput('minSize', {
type: NumberSchema
type: {
...NumberSchema,
default: 16,
description: 'Minimum size in pixels'
}
});
this.addInput('maxSize', {
type: NumberSchema
type: {
...NumberSchema,
default: 24,
description: 'Maximum size in pixels'
}
});
this.addInput('minViewport', {
type: NumberSchema
type: {
...NumberSchema,
default: 320,
description: 'Minimum viewport width in pixels'
}
});
this.addInput('maxViewport', {
type: NumberSchema
type: {
...NumberSchema,
default: 1920,
description: 'Maximum viewport width in pixels'
}
});
this.addInput('viewport', {
type: NumberSchema
type: {
...NumberSchema,
default: 768,
description: 'Current viewport width in pixels'
}
});
this.addInput('precision', {
type: {
...NumberSchema,
default: 2,
minimum: 0,
description: 'Number of decimal places in the output'
}
});
this.addOutput('value', {
type: NumberSchema
});
}

execute(): void | Promise<void> {
const { minSize, maxSize, minViewport, maxViewport, viewport } =
const { minSize, maxSize, minViewport, maxViewport, viewport, precision } =
this.getAllInputs();
const fontV = (100 * (maxSize - minSize)) / (maxViewport - minViewport);

// Get the actual min and max values regardless of input order
const actualMinSize = Math.min(minSize, maxSize);
const actualMaxSize = Math.max(minSize, maxSize);
const actualMinViewport = Math.min(minViewport, maxViewport);
const actualMaxViewport = Math.max(minViewport, maxViewport);

// Handle equal viewport case
if (actualMinViewport === actualMaxViewport) {
this.outputs.value.set(setToPrecision(actualMinSize, precision));
return;
}

const fontV =
(100 * (actualMaxSize - actualMinSize)) /
(actualMaxViewport - actualMinViewport);
const fontR =
(minViewport * maxSize - maxViewport * minSize) /
(minViewport - maxViewport);
(actualMinViewport * actualMaxSize - actualMaxViewport * actualMinSize) /
(actualMinViewport - actualMaxViewport);
const fluid = (viewport / 100) * fontV + fontR;
const clamped = Math.min(maxSize, Math.max(minSize, fluid));
const clamped = Math.min(actualMaxSize, Math.max(actualMinSize, fluid));

this.outputs.value.set(clamped);
this.outputs.value.set(setToPrecision(clamped, precision));
}
}
69 changes: 69 additions & 0 deletions packages/graph-engine/tests/suites/nodes/math/fluid.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Graph } from '../../../../src/graph/graph.js';
import { describe, expect, test } from 'vitest';
import Node from '../../../../src/nodes/math/fluid.js';

describe('math/fluid', () => {
test('calculates fluid value correctly with normal order', () => {
const graph = new Graph();
const node = new Node({ graph });

node.inputs.minSize.setValue(16);
node.inputs.maxSize.setValue(32);
node.inputs.minViewport.setValue(200);
node.inputs.maxViewport.setValue(1200);
node.inputs.viewport.setValue(700);
node.inputs.precision.setValue(2);

node.execute();

expect(node.outputs.value.value).toBe(24);
});

test('handles reversed min/max size values', () => {
const graph = new Graph();
const node = new Node({ graph });

node.inputs.minSize.setValue(32);
node.inputs.maxSize.setValue(16);
node.inputs.minViewport.setValue(200);
node.inputs.maxViewport.setValue(1200);
node.inputs.viewport.setValue(700);
node.inputs.precision.setValue(2);

node.execute();

expect(node.outputs.value.value).toBe(24);
});

test('respects different precision values', () => {
const graph = new Graph();
const node = new Node({ graph });

node.inputs.minSize.setValue(16);
node.inputs.maxSize.setValue(32);
node.inputs.minViewport.setValue(320);
node.inputs.maxViewport.setValue(1200);
node.inputs.viewport.setValue(768);
node.inputs.precision.setValue(0);

node.execute();

expect(node.outputs.value.value).toBe(24);
});

test('handles equal viewport values', () => {
const graph = new Graph();
const node = new Node({ graph });

node.inputs.minSize.setValue(16);
node.inputs.maxSize.setValue(32);
node.inputs.minViewport.setValue(800);
node.inputs.maxViewport.setValue(800);
node.inputs.viewport.setValue(800);
node.inputs.precision.setValue(2);

node.execute();

expect(node.outputs.value.value).toBe(16);
});
});
Loading