@times-design-system/components-wordpress 1.7.0 → 1.8.1-alpha.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,209 @@
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 and optional variant
59
+ *
60
+ /**
61
+ * Get icon SVG content from icon data store
62
+ *
63
+ * Icon names include variant prefix: 'filled_arrow_left', 'outlined_check'
64
+ *
65
+ * @param string $icon_name Icon name with variant (e.g., 'filled_arrow_left', 'outlined_check')
66
+ * @return string|false SVG content or false if not found
67
+ */
68
+ function tds_get_icon_svg($icon_name) {
69
+ if (empty($icon_name)) {
70
+ return false;
71
+ }
72
+
73
+ $icon_data = tds_load_icon_data();
74
+ if (!$icon_data || !isset($icon_data['icons'])) {
75
+ return false;
76
+ }
77
+
78
+ // Sanitize the icon name (includes variant prefix)
79
+ $icon_name = sanitize_key($icon_name);
80
+
81
+ // Look up icon directly (icon name already includes variant)
82
+ if (isset($icon_data['icons'][$icon_name])) {
83
+ return $icon_data['icons'][$icon_name];
84
+ }
85
+
86
+ return false;
87
+ }
88
+
89
+ /**
90
+ * Render an inline SVG icon with optional sizing
91
+ *
92
+ * Icon names include variant prefix (e.g., 'filled_accessibility', 'outlined_check')
93
+ *
94
+ * @param string $icon_name Icon name with variant (e.g., 'filled_circle', 'outlined_circle')
95
+ * @param string $size_name Size name (e.g., 'medium', 'large')
96
+ * @param string $class Optional CSS class to add to the SVG
97
+ * @return string HTML SVG string or empty string if icon not found
98
+ */
99
+ function tds_render_icon($icon_name, $size_name = 'medium', $class = '') {
100
+ if (empty($icon_name)) {
101
+ return '';
102
+ }
103
+
104
+ $svg_content = tds_get_icon_svg($icon_name);
105
+ if (!$svg_content) {
106
+ // Icon not found - log debug info if WP_DEBUG is enabled
107
+ if (defined('WP_DEBUG') && WP_DEBUG) {
108
+ error_log("Icon not found: {$variant}-{$icon_name}");
109
+ }
110
+ return '';
111
+ }
112
+
113
+ // Get dimensions for the size
114
+ $size_name = sanitize_key($size_name ?? 'medium');
115
+ $dimensions = ICON_SIZE_MAP[$size_name] ?? ICON_SIZE_MAP['medium'];
116
+
117
+ // Parse SVG to add width/height attributes
118
+ $svg_content = tds_inject_svg_dimensions($svg_content, $dimensions['width'], $dimensions['height'], $class);
119
+
120
+ return $svg_content;
121
+ }
122
+
123
+ /**
124
+ * Inject width/height attributes into SVG string
125
+ *
126
+ * Handles both existing SVG elements and injects sizing.
127
+ * Also adds optional CSS class and replaces color attributes with currentcolor.
128
+ *
129
+ * @param string $svg SVG content
130
+ * @param int $width Width in pixels
131
+ * @param int $height Height in pixels
132
+ * @param string $class Optional CSS class
133
+ * @return string Modified SVG
134
+ */
135
+ function tds_inject_svg_dimensions($svg, $width, $height, $class = '') {
136
+ // If SVG doesn't have width/height, inject them
137
+ if (strpos($svg, 'width=') === false) {
138
+ $svg = str_replace('<svg', sprintf('<svg width="%d" height="%d"', $width, $height), $svg);
139
+ }
140
+
141
+ // Replace fill attributes with currentcolor
142
+ $svg = preg_replace('/fill="[^"]*"/i', 'fill="currentcolor"', $svg);
143
+
144
+ // Replace stroke attributes with currentcolor
145
+ $svg = preg_replace('/stroke="[^"]*"/i', 'stroke="currentcolor"', $svg);
146
+
147
+ // Handle fill in style attributes: style="fill: #xxx" or style="...fill: #xxx..."
148
+ $svg = preg_replace('/style="([^"]*)fill:\s*[^;}"]+/i', 'style="$1fill: currentcolor', $svg);
149
+
150
+ // Handle stroke in style attributes: style="stroke: #xxx" or style="...stroke: #xxx..."
151
+ $svg = preg_replace('/style="([^"]*)stroke:\s*[^;}"]+/i', 'style="$1stroke: currentcolor', $svg);
152
+
153
+ // Add class if provided
154
+ if (!empty($class)) {
155
+ $class_attr = sanitize_html_class($class);
156
+ if (preg_match('/class="([^"]*)"/', $svg, $matches)) {
157
+ // Class already exists, append to it
158
+ $existing_class = $matches[1];
159
+ $svg = str_replace(
160
+ sprintf('class="%s"', $existing_class),
161
+ sprintf('class="%s %s"', $existing_class, $class_attr),
162
+ $svg
163
+ );
164
+ } else {
165
+ // No class attribute, add one
166
+ $svg = str_replace('<svg', sprintf('<svg class="%s"', $class_attr), $svg);
167
+ }
168
+ }
169
+
170
+ return $svg;
171
+ }
172
+
173
+ /**
174
+ * Render icon in a wrapper span with sizing classes
175
+ *
176
+ * Useful for blocks that position icons within containers.
177
+ *
178
+ * @param string $icon_name Icon name
179
+ * @param string $size_name Size name (medium, large, etc)
180
+ * @param string $position Position modifier for BEM class (e.g., 'left', 'right')
181
+ * @param string $component Component name for BEM (e.g., 'button', 'chip')
182
+ * @return string HTML with icon span and inline SVG
183
+ */
184
+ function tds_render_icon_wrapper($icon_name, $size_name = 'medium', $position = '', $component = '') {
185
+ if (empty($icon_name)) {
186
+ return '';
187
+ }
188
+
189
+ $svg = tds_render_icon($icon_name, $size_name);
190
+ if (empty($svg)) {
191
+ return '';
192
+ }
193
+
194
+ $class_parts = ['tds-component__icon'];
195
+ if ($position) {
196
+ $class_parts[] = 'tds-component__icon--' . sanitize_html_class($position);
197
+ }
198
+ if ($component) {
199
+ $class_parts[] = 'tds-' . sanitize_html_class($component) . '__icon';
200
+ }
201
+
202
+ $wrapper_class = implode(' ', $class_parts);
203
+
204
+ return sprintf(
205
+ '<span class="%s">%s</span>',
206
+ esc_attr($wrapper_class),
207
+ wp_kses_post($svg)
208
+ );
209
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@times-design-system/components-wordpress",
3
- "version": "1.7.0",
3
+ "version": "1.8.1-alpha.0+66ac66b0",
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.6.0",
68
+ "@times-design-system/theme-scss": "^2.6.1-alpha.2+66ac66b0",
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.1-alpha.77+66ac66b0",
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": "29e19963c74c5a18155ee4267704e0916f58c81c"
98
+ "gitHead": "66ac66b0ffc921ff609182674bb573de41fe7e36"
97
99
  }