@times-design-system/components-wordpress 1.6.0 → 1.8.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.
@@ -0,0 +1,183 @@
1
+ <?php
2
+ /**
3
+ * Icon Rendering Utility
4
+ *
5
+ * Provides functions for rendering inline SVG icons with sizing support.
6
+ * Icons are loaded from dist/icon-data.json (generated by extract-icons.cjs).
7
+ *
8
+ * Ensures parity with components-react Icon component sizing.
9
+ *
10
+ * @package TimesDesignSystem\ComponentsWordPress
11
+ */
12
+
13
+ /**
14
+ * Icon size mapping (matches components-react ICON_SIZE_MAP)
15
+ *
16
+ * @var array
17
+ */
18
+ const ICON_SIZE_MAP = [
19
+ '2xsmall' => ['width' => 12, 'height' => 12],
20
+ 'xsmall' => ['width' => 16, 'height' => 16],
21
+ 'small' => ['width' => 20, 'height' => 20],
22
+ 'medium' => ['width' => 24, 'height' => 24],
23
+ 'large' => ['width' => 28, 'height' => 28],
24
+ 'xlarge' => ['width' => 32, 'height' => 32],
25
+ '2xlarge' => ['width' => 40, 'height' => 40],
26
+ ];
27
+
28
+ /**
29
+ * Load icon data from JSON file
30
+ *
31
+ * Caches the data in a static variable to avoid repeated file reads.
32
+ *
33
+ * @return array|false Icon data array with 'icons' key, or false on failure
34
+ */
35
+ function tds_load_icon_data() {
36
+ static $icon_data = null;
37
+
38
+ if ($icon_data !== null) {
39
+ return $icon_data;
40
+ }
41
+
42
+ $plugin_dir = dirname(dirname(__DIR__));
43
+ $icon_data_path = $plugin_dir . '/dist/icon-data.json';
44
+
45
+ if (!file_exists($icon_data_path)) {
46
+ // If icon data hasn't been extracted yet, log a warning and return false
47
+ // This is normal during first install or if build hasn't been run
48
+ return false;
49
+ }
50
+
51
+ $json_content = file_get_contents($icon_data_path);
52
+ $icon_data = json_decode($json_content, true);
53
+
54
+ return $icon_data ?: false;
55
+ }
56
+
57
+ /**
58
+ * Get icon SVG by name
59
+ *
60
+ * @param string $icon_name Icon name (e.g., 'arrow-left', 'check')
61
+ * @return string|false SVG content or false if not found
62
+ */
63
+ function tds_get_icon_svg($icon_name) {
64
+ if (empty($icon_name)) {
65
+ return false;
66
+ }
67
+
68
+ $icon_data = tds_load_icon_data();
69
+ if (!$icon_data || !isset($icon_data['icons'])) {
70
+ return false;
71
+ }
72
+
73
+ $icon_name = sanitize_key($icon_name);
74
+ return $icon_data['icons'][$icon_name] ?? false;
75
+ }
76
+
77
+ /**
78
+ * Render an inline SVG icon with optional sizing
79
+ *
80
+ * @param string $icon_name Icon name (e.g., 'arrow-left')
81
+ * @param string $size_name Size name (e.g., 'medium', 'large')
82
+ * @param string $class Optional CSS class to add to the SVG
83
+ * @return string HTML SVG string or empty string if icon not found
84
+ */
85
+ function tds_render_icon($icon_name, $size_name = 'medium', $class = '') {
86
+ if (empty($icon_name)) {
87
+ return '';
88
+ }
89
+
90
+ $svg_content = tds_get_icon_svg($icon_name);
91
+ if (!$svg_content) {
92
+ // Icon not found - log debug info if WP_DEBUG is enabled
93
+ if (defined('WP_DEBUG') && WP_DEBUG) {
94
+ error_log("Icon not found: {$icon_name}");
95
+ }
96
+ return '';
97
+ }
98
+
99
+ // Get dimensions for the size
100
+ $size_name = sanitize_key($size_name ?? 'medium');
101
+ $dimensions = ICON_SIZE_MAP[$size_name] ?? ICON_SIZE_MAP['medium'];
102
+
103
+ // Parse SVG to add width/height attributes
104
+ $svg_content = tds_inject_svg_dimensions($svg_content, $dimensions['width'], $dimensions['height'], $class);
105
+
106
+ return $svg_content;
107
+ }
108
+
109
+ /**
110
+ * Inject width/height attributes into SVG string
111
+ *
112
+ * Handles both existing SVG elements and injects sizing.
113
+ * Also adds optional CSS class.
114
+ *
115
+ * @param string $svg SVG content
116
+ * @param int $width Width in pixels
117
+ * @param int $height Height in pixels
118
+ * @param string $class Optional CSS class
119
+ * @return string Modified SVG
120
+ */
121
+ function tds_inject_svg_dimensions($svg, $width, $height, $class = '') {
122
+ // If SVG doesn't have width/height, inject them
123
+ if (strpos($svg, 'width=') === false) {
124
+ $svg = str_replace('<svg', sprintf('<svg width="%d" height="%d"', $width, $height), $svg);
125
+ }
126
+
127
+ // Add class if provided
128
+ if (!empty($class)) {
129
+ $class_attr = sanitize_html_class($class);
130
+ if (preg_match('/class="([^"]*)"/', $svg, $matches)) {
131
+ // Class already exists, append to it
132
+ $existing_class = $matches[1];
133
+ $svg = str_replace(
134
+ sprintf('class="%s"', $existing_class),
135
+ sprintf('class="%s %s"', $existing_class, $class_attr),
136
+ $svg
137
+ );
138
+ } else {
139
+ // No class attribute, add one
140
+ $svg = str_replace('<svg', sprintf('<svg class="%s"', $class_attr), $svg);
141
+ }
142
+ }
143
+
144
+ return $svg;
145
+ }
146
+
147
+ /**
148
+ * Render icon in a wrapper span with sizing classes
149
+ *
150
+ * Useful for blocks that position icons within containers.
151
+ *
152
+ * @param string $icon_name Icon name
153
+ * @param string $size_name Size name (medium, large, etc)
154
+ * @param string $position Position modifier for BEM class (e.g., 'left', 'right')
155
+ * @param string $component Component name for BEM (e.g., 'button', 'chip')
156
+ * @return string HTML with icon span and inline SVG
157
+ */
158
+ function tds_render_icon_wrapper($icon_name, $size_name = 'medium', $position = '', $component = '') {
159
+ if (empty($icon_name)) {
160
+ return '';
161
+ }
162
+
163
+ $svg = tds_render_icon($icon_name, $size_name);
164
+ if (empty($svg)) {
165
+ return '';
166
+ }
167
+
168
+ $class_parts = ['tds-component__icon'];
169
+ if ($position) {
170
+ $class_parts[] = 'tds-component__icon--' . sanitize_html_class($position);
171
+ }
172
+ if ($component) {
173
+ $class_parts[] = 'tds-' . sanitize_html_class($component) . '__icon';
174
+ }
175
+
176
+ $wrapper_class = implode(' ', $class_parts);
177
+
178
+ return sprintf(
179
+ '<span class="%s">%s</span>',
180
+ esc_attr($wrapper_class),
181
+ wp_kses_post($svg)
182
+ );
183
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@times-design-system/components-wordpress",
3
- "version": "1.6.0",
3
+ "version": "1.8.0",
4
4
  "type": "module",
5
5
  "description": "Times Design System Gutenberg blocks for WordPress Full Site Editing",
6
6
  "keywords": [
@@ -32,10 +32,11 @@
32
32
  "requiresPhp": "7.4"
33
33
  },
34
34
  "scripts": {
35
- "build": "npm run clean && npm run build:rollup && npm run build:plugin",
35
+ "build": "npm run clean && npm run extract:icons && npm run build:rollup && npm run build:plugin",
36
36
  "build:rollup": "rollup -c",
37
37
  "build:plugin": "node scripts/build-plugin.cjs",
38
38
  "clean": "rimraf dist",
39
+ "extract:icons": "node scripts/extract-icons.cjs",
39
40
  "dev": "rollup -c --watch",
40
41
  "prepublishOnly": "npm run build",
41
42
  "test": "vitest --run",
@@ -64,7 +65,7 @@
64
65
  "access": "public"
65
66
  },
66
67
  "dependencies": {
67
- "@times-design-system/theme-scss": "^2.5.1",
68
+ "@times-design-system/theme-scss": "^2.6.0",
68
69
  "@wordpress/block-editor": "^12.0.0",
69
70
  "@wordpress/blocks": "^12.0.0",
70
71
  "@wordpress/components": "^25.0.0",
@@ -82,6 +83,7 @@
82
83
  "@testing-library/jest-dom": "^6.1.5",
83
84
  "@testing-library/react": "^14.1.2",
84
85
  "@testing-library/user-event": "^14.5.1",
86
+ "@times-design-system/icons": "^1.2.0",
85
87
  "@types/react": "19.2.5",
86
88
  "@vitest/ui": "^1.0.4",
87
89
  "jsdom": "^23.0.0",
@@ -93,5 +95,5 @@
93
95
  "typescript": "^5.0.0",
94
96
  "vitest": "^1.0.4"
95
97
  },
96
- "gitHead": "96aae79e15100c0ddf7ce0e9defa5283c2d35803"
98
+ "gitHead": "0eead454d09b74d39f75b6421aa5a9a5bc307566"
97
99
  }