@contentful/experiences-components-react 3.1.1 → 3.2.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1216,35 +1216,69 @@ const breakpointsRefinement = (value, ctx) => {
1216
1216
  code: z.ZodIssueCode.custom,
1217
1217
  message: `The first breakpoint should include the following attributes: { "query": "*" }`,
1218
1218
  });
1219
+ return;
1219
1220
  }
1220
- const hasDuplicateIds = value.some((currentBreakpoint, currentBreakpointIndex) => {
1221
- // check if the current breakpoint id is found in the rest of the array
1222
- const breakpointIndex = value.findIndex((breakpoint) => breakpoint.id === currentBreakpoint.id);
1223
- return breakpointIndex !== currentBreakpointIndex;
1224
- });
1221
+ // Return early if there's only one generic breakpoint
1222
+ const hasNoBreakpointsStrategy = value.length === 1;
1223
+ if (hasNoBreakpointsStrategy) {
1224
+ return;
1225
+ }
1226
+ // Check if any breakpoint id occurs twice
1227
+ const ids = value.map((breakpoint) => breakpoint.id);
1228
+ const hasDuplicateIds = new Set(ids).size !== ids.length;
1225
1229
  if (hasDuplicateIds) {
1226
1230
  ctx.addIssue({
1227
1231
  code: z.ZodIssueCode.custom,
1228
1232
  message: `Breakpoint IDs must be unique`,
1229
1233
  });
1234
+ return;
1230
1235
  }
1231
- // Extract the queries boundary by removing the special characters around it
1232
- const queries = value.map((bp) => bp.query === '*' ? bp.query : parseInt(bp.query.replace(/px|<|>/, '')));
1233
- // sort updates queries array in place so we need to create a copy
1234
- const originalQueries = [...queries];
1235
- queries.sort((q1, q2) => {
1236
- if (q1 === '*') {
1237
- return -1;
1236
+ // Skip the first one which is guaranteed to be a wildcard query
1237
+ const nonBaseBreakpoints = value.slice(1);
1238
+ const isMobileFirstStrategy = nonBaseBreakpoints[0].query.startsWith('>');
1239
+ const isDesktopFirstStrategy = nonBaseBreakpoints[0].query.startsWith('<');
1240
+ if (isMobileFirstStrategy) {
1241
+ const areOperatorsEqual = nonBaseBreakpoints.every(({ query }) => query.startsWith('>'));
1242
+ if (!areOperatorsEqual) {
1243
+ ctx.addIssue({
1244
+ code: z.ZodIssueCode.custom,
1245
+ message: `Breakpoint queries must be in the format ">[size]px" for mobile-first strategy`,
1246
+ });
1247
+ }
1248
+ // Extract the queries boundary by removing the special characters around it
1249
+ const queries = nonBaseBreakpoints.map((bp) => parseInt(bp.query.replace(/px|<|>/, '')));
1250
+ // Starting with the third breakpoint, check that every query is higher than the one above
1251
+ const isIncreasing = queries.every((value, index, array) => index === 0 || value > array[index - 1]);
1252
+ if (!isIncreasing) {
1253
+ ctx.addIssue({
1254
+ code: z.ZodIssueCode.custom,
1255
+ message: `When using a mobile-first strategy, all breakpoints must have strictly increasing pixel values`,
1256
+ });
1238
1257
  }
1239
- if (q2 === '*') {
1240
- return 1;
1258
+ }
1259
+ else if (isDesktopFirstStrategy) {
1260
+ const areOperatorsEqual = nonBaseBreakpoints.every(({ query }) => query.startsWith('<'));
1261
+ if (!areOperatorsEqual) {
1262
+ ctx.addIssue({
1263
+ code: z.ZodIssueCode.custom,
1264
+ message: `Breakpoint queries must be in the format "<[size]px" for desktop-first strategy`,
1265
+ });
1241
1266
  }
1242
- return q1 > q2 ? -1 : 1;
1243
- });
1244
- if (originalQueries.join('') !== queries.join('')) {
1267
+ // Extract the queries boundary by removing the special characters around it
1268
+ const queries = nonBaseBreakpoints.map((bp) => parseInt(bp.query.replace(/px|<|>/, '')));
1269
+ // Starting with the third breakpoint, check that every query is lower than the one above
1270
+ const isDecreasing = queries.every((value, index, array) => index === 0 || value < array[index - 1]);
1271
+ if (!isDecreasing) {
1272
+ ctx.addIssue({
1273
+ code: z.ZodIssueCode.custom,
1274
+ message: `When using a desktop-first strategy, all breakpoints must have strictly decreasing pixel values`,
1275
+ });
1276
+ }
1277
+ }
1278
+ else if (!isMobileFirstStrategy && !isDesktopFirstStrategy) {
1245
1279
  ctx.addIssue({
1246
1280
  code: z.ZodIssueCode.custom,
1247
- message: `Breakpoints should be ordered from largest to smallest pixel value`,
1281
+ message: `You may only use a mobile-first or desktop-first strategy for breakpoints using '<' or '>' queries`,
1248
1282
  });
1249
1283
  }
1250
1284
  };
@@ -1303,7 +1337,7 @@ const ParametersSchema = z.record(propertyKeySchema, ParameterSchema);
1303
1337
  const BreakpointSchema = z
1304
1338
  .object({
1305
1339
  id: propertyKeySchema,
1306
- query: z.string().regex(/^\*$|^<[0-9*]+px$/),
1340
+ query: z.string().regex(/^\*$|^[<>][0-9*]+px$/),
1307
1341
  previewSize: z.string(),
1308
1342
  displayName: z.string(),
1309
1343
  displayIcon: z.enum(['desktop', 'tablet', 'mobile']).optional(),