|
| 1 | +import { useState } from "react"; |
| 2 | +import { |
| 3 | + EnhancedTooltip, |
| 4 | + Grid, |
| 5 | + SmallToggleButton, |
| 6 | + theme, |
| 7 | +} from "@webstudio-is/design-system"; |
| 8 | +import { |
| 9 | + toValue, |
| 10 | + type CssProperty, |
| 11 | + type StyleValue, |
| 12 | + type TupleValue, |
| 13 | +} from "@webstudio-is/css-engine"; |
| 14 | +import { |
| 15 | + Link2Icon, |
| 16 | + Link2UnlinkedIcon, |
| 17 | + XAxisIcon, |
| 18 | + YAxisIcon, |
| 19 | + ZAxisRotateIcon, |
| 20 | +} from "@webstudio-is/icons"; |
| 21 | +import { CssValueInputContainer } from "~/builder/features/style-panel/shared/css-value-input"; |
| 22 | +import { $availableUnitVariables } from "~/builder/features/style-panel/shared/model"; |
| 23 | +import type { StyleUpdateOptions } from "~/builder/features/style-panel/shared/use-style-data"; |
| 24 | +import { FieldLabel } from "../../property-label"; |
| 25 | + |
| 26 | +const isTupleItem = (value: StyleValue): value is TupleValue["value"][number] => |
| 27 | + value.type === "unit" || value.type === "var" || value.type === "unparsed"; |
| 28 | + |
| 29 | +export const transformProperties: CssProperty[] = [ |
| 30 | + "translate", |
| 31 | + "scale", |
| 32 | + "rotate", |
| 33 | + "opacity", |
| 34 | +]; |
| 35 | + |
| 36 | +// gap linked gap |
| 37 | +const CONTROLS_GAP = 4 + 16 + 4; |
| 38 | + |
| 39 | +export const AnimationTransforms = ({ |
| 40 | + styles, |
| 41 | + onUpdate, |
| 42 | + onDelete, |
| 43 | +}: { |
| 44 | + styles: Record<CssProperty, undefined | StyleValue>; |
| 45 | + onUpdate: ( |
| 46 | + property: CssProperty, |
| 47 | + value: StyleValue, |
| 48 | + options?: StyleUpdateOptions |
| 49 | + ) => void; |
| 50 | + onDelete: (property: CssProperty, options?: StyleUpdateOptions) => void; |
| 51 | +}) => { |
| 52 | + const [isScaleLinked, setIsScaleLinked] = useState( |
| 53 | + styles.scale?.type !== "tuple" || |
| 54 | + toValue(styles.scale.value[0]) === toValue(styles.scale.value[1]) |
| 55 | + ); |
| 56 | + let translateX: StyleValue = { type: "unit", value: 0, unit: "px" }; |
| 57 | + let translateY: StyleValue = { type: "unit", value: 0, unit: "px" }; |
| 58 | + if (styles.translate?.type === "tuple") { |
| 59 | + [translateX, translateY = translateY] = styles.translate.value; |
| 60 | + } |
| 61 | + let scaleX: StyleValue = { type: "unit", value: 100, unit: "%" }; |
| 62 | + let scaleY: StyleValue = { type: "unit", value: 100, unit: "%" }; |
| 63 | + if (styles.scale?.type === "tuple") { |
| 64 | + [scaleX, scaleY = scaleY] = styles.scale.value; |
| 65 | + } |
| 66 | + let rotateZ: StyleValue = { type: "unit", value: 0, unit: "deg" }; |
| 67 | + if (styles.rotate?.type === "tuple") { |
| 68 | + if (isTupleItem(styles.rotate.value[0])) { |
| 69 | + rotateZ = styles.rotate.value[0]; |
| 70 | + } |
| 71 | + } |
| 72 | + return ( |
| 73 | + <Grid gap={2} css={{ padding: theme.panel.padding }}> |
| 74 | + <Grid gap={1}> |
| 75 | + <FieldLabel |
| 76 | + resettable={styles.translate !== undefined} |
| 77 | + onReset={() => onDelete("translate")} |
| 78 | + > |
| 79 | + Translate |
| 80 | + </FieldLabel> |
| 81 | + <Grid columns={2} css={{ gap: CONTROLS_GAP }}> |
| 82 | + <CssValueInputContainer |
| 83 | + property="translate" |
| 84 | + styleSource={styles.translate ? "local" : "default"} |
| 85 | + icon={<XAxisIcon />} |
| 86 | + getOptions={() => $availableUnitVariables.get()} |
| 87 | + value={translateX} |
| 88 | + onUpdate={(value, options) => { |
| 89 | + if (value.type === "tuple") { |
| 90 | + onUpdate( |
| 91 | + "translate", |
| 92 | + { type: "tuple", value: [value.value[0], translateY] }, |
| 93 | + options |
| 94 | + ); |
| 95 | + } |
| 96 | + // for scrabbing |
| 97 | + if (value.type === "unit") { |
| 98 | + onUpdate( |
| 99 | + "translate", |
| 100 | + { type: "tuple", value: [value, translateY] }, |
| 101 | + options |
| 102 | + ); |
| 103 | + } |
| 104 | + }} |
| 105 | + onDelete={(options) => { |
| 106 | + const value = styles.translate ?? { |
| 107 | + type: "tuple", |
| 108 | + value: [translateX, translateY], |
| 109 | + }; |
| 110 | + onUpdate("translate", value, options); |
| 111 | + }} |
| 112 | + /> |
| 113 | + <CssValueInputContainer |
| 114 | + property="translate" |
| 115 | + styleSource={styles.translate ? "local" : "default"} |
| 116 | + icon={<YAxisIcon />} |
| 117 | + getOptions={() => $availableUnitVariables.get()} |
| 118 | + value={translateY} |
| 119 | + onUpdate={(value, options) => { |
| 120 | + if (value.type === "tuple") { |
| 121 | + onUpdate( |
| 122 | + "translate", |
| 123 | + { type: "tuple", value: [translateX, value.value[0]] }, |
| 124 | + options |
| 125 | + ); |
| 126 | + } |
| 127 | + // for scrabbing |
| 128 | + if (value.type === "unit") { |
| 129 | + onUpdate( |
| 130 | + "translate", |
| 131 | + { type: "tuple", value: [translateX, value] }, |
| 132 | + options |
| 133 | + ); |
| 134 | + } |
| 135 | + }} |
| 136 | + onDelete={(options) => { |
| 137 | + const value = styles.translate ?? { |
| 138 | + type: "tuple", |
| 139 | + value: [translateX, translateY], |
| 140 | + }; |
| 141 | + onUpdate("translate", value, options); |
| 142 | + }} |
| 143 | + /> |
| 144 | + </Grid> |
| 145 | + </Grid> |
| 146 | + |
| 147 | + <Grid gap={1}> |
| 148 | + <FieldLabel |
| 149 | + resettable={styles.scale !== undefined} |
| 150 | + onReset={() => onDelete("scale")} |
| 151 | + > |
| 152 | + Scale |
| 153 | + </FieldLabel> |
| 154 | + <Grid |
| 155 | + gap={1} |
| 156 | + align="center" |
| 157 | + css={{ gridTemplateColumns: "1fr 16px 1fr" }} |
| 158 | + > |
| 159 | + <CssValueInputContainer |
| 160 | + property="scale" |
| 161 | + styleSource={styles.scale ? "local" : "default"} |
| 162 | + icon={<XAxisIcon />} |
| 163 | + getOptions={() => $availableUnitVariables.get()} |
| 164 | + value={scaleX} |
| 165 | + onUpdate={(value, options) => { |
| 166 | + if (value.type === "tuple") { |
| 167 | + const newValue: StyleValue = { |
| 168 | + type: "tuple", |
| 169 | + value: isScaleLinked |
| 170 | + ? [value.value[0], value.value[0]] |
| 171 | + : [value.value[0], scaleY], |
| 172 | + }; |
| 173 | + onUpdate("scale", newValue, options); |
| 174 | + } |
| 175 | + // for scrabbing |
| 176 | + if (value.type === "unit") { |
| 177 | + const newValue: StyleValue = { |
| 178 | + type: "tuple", |
| 179 | + value: isScaleLinked ? [value, value] : [value, scaleY], |
| 180 | + }; |
| 181 | + onUpdate("scale", newValue, options); |
| 182 | + } |
| 183 | + }} |
| 184 | + onDelete={(options) => { |
| 185 | + if (isScaleLinked) { |
| 186 | + onDelete("scale", options); |
| 187 | + } else { |
| 188 | + const value = styles.scale ?? { |
| 189 | + type: "tuple", |
| 190 | + value: [scaleX, scaleY], |
| 191 | + }; |
| 192 | + onUpdate("scale", value, options); |
| 193 | + } |
| 194 | + }} |
| 195 | + /> |
| 196 | + <EnhancedTooltip |
| 197 | + content={isScaleLinked ? "Unlink scale axes" : "Link scale axes"} |
| 198 | + > |
| 199 | + <SmallToggleButton |
| 200 | + variant="normal" |
| 201 | + icon={isScaleLinked ? <Link2Icon /> : <Link2UnlinkedIcon />} |
| 202 | + pressed={isScaleLinked} |
| 203 | + onPressedChange={(isPressed) => { |
| 204 | + setIsScaleLinked(isPressed); |
| 205 | + if (isPressed) { |
| 206 | + onUpdate("scale", { |
| 207 | + type: "tuple", |
| 208 | + value: [scaleX, scaleX], |
| 209 | + }); |
| 210 | + } |
| 211 | + }} |
| 212 | + /> |
| 213 | + </EnhancedTooltip> |
| 214 | + <CssValueInputContainer |
| 215 | + property="scale" |
| 216 | + styleSource={styles.scale ? "local" : "default"} |
| 217 | + icon={<YAxisIcon />} |
| 218 | + getOptions={() => $availableUnitVariables.get()} |
| 219 | + value={scaleY} |
| 220 | + onUpdate={(value, options) => { |
| 221 | + if (value.type === "tuple") { |
| 222 | + const newValue: StyleValue = { |
| 223 | + type: "tuple", |
| 224 | + value: isScaleLinked |
| 225 | + ? [value.value[0], value.value[0]] |
| 226 | + : [scaleX, value.value[0]], |
| 227 | + }; |
| 228 | + onUpdate("scale", newValue, options); |
| 229 | + } |
| 230 | + // for scrabbing |
| 231 | + if (value.type === "unit") { |
| 232 | + const newValue: StyleValue = { |
| 233 | + type: "tuple", |
| 234 | + value: isScaleLinked ? [value, value] : [scaleX, value], |
| 235 | + }; |
| 236 | + onUpdate("scale", newValue, options); |
| 237 | + } |
| 238 | + }} |
| 239 | + onDelete={(options) => { |
| 240 | + if (isScaleLinked) { |
| 241 | + onDelete("scale", options); |
| 242 | + } else { |
| 243 | + const value = styles.scale ?? { |
| 244 | + type: "tuple", |
| 245 | + value: [scaleX, scaleY], |
| 246 | + }; |
| 247 | + onUpdate("scale", value, options); |
| 248 | + } |
| 249 | + }} |
| 250 | + /> |
| 251 | + </Grid> |
| 252 | + </Grid> |
| 253 | + |
| 254 | + <Grid columns={2} css={{ gap: CONTROLS_GAP }}> |
| 255 | + <Grid gap={1}> |
| 256 | + <FieldLabel |
| 257 | + resettable={styles.rotate !== undefined} |
| 258 | + onReset={() => onDelete("rotate")} |
| 259 | + > |
| 260 | + Rotate |
| 261 | + </FieldLabel> |
| 262 | + <CssValueInputContainer |
| 263 | + property="rotate" |
| 264 | + styleSource={styles.rotate ? "local" : "default"} |
| 265 | + icon={<ZAxisRotateIcon />} |
| 266 | + getOptions={() => $availableUnitVariables.get()} |
| 267 | + value={rotateZ} |
| 268 | + onUpdate={(value, options) => { |
| 269 | + if (value.type === "tuple" && isTupleItem(value.value[0])) { |
| 270 | + const [rotateZ] = value.value; |
| 271 | + onUpdate( |
| 272 | + "rotate", |
| 273 | + { type: "tuple", value: [rotateZ] }, |
| 274 | + options |
| 275 | + ); |
| 276 | + } else if (isTupleItem(value)) { |
| 277 | + // for scrabbing |
| 278 | + onUpdate("rotate", { type: "tuple", value: [value] }, options); |
| 279 | + } else { |
| 280 | + onDelete("rotate", options); |
| 281 | + } |
| 282 | + }} |
| 283 | + onDelete={(options) => onDelete("rotate", options)} |
| 284 | + /> |
| 285 | + </Grid> |
| 286 | + <Grid gap={1}> |
| 287 | + <FieldLabel |
| 288 | + resettable={styles.opacity !== undefined} |
| 289 | + onReset={() => onDelete("opacity")} |
| 290 | + > |
| 291 | + Opacity |
| 292 | + </FieldLabel> |
| 293 | + <CssValueInputContainer |
| 294 | + property="opacity" |
| 295 | + styleSource={styles.opacity ? "local" : "default"} |
| 296 | + getOptions={() => $availableUnitVariables.get()} |
| 297 | + value={styles.opacity ?? { type: "unit", value: 100, unit: "%" }} |
| 298 | + onUpdate={(value, options) => { |
| 299 | + onUpdate("opacity", value, options); |
| 300 | + }} |
| 301 | + onDelete={(options) => onDelete("opacity", options)} |
| 302 | + /> |
| 303 | + </Grid> |
| 304 | + </Grid> |
| 305 | + </Grid> |
| 306 | + ); |
| 307 | +}; |
0 commit comments