directionalProperty.scss 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // =======================================================================
  2. // 方向属性核心生成器 (支持所有方向相关属性)
  3. // =======================================================================
  4. @mixin directional-property(
  5. $property,
  6. $values,
  7. $exclude: ()
  8. ) {
  9. // 属性类型分组
  10. $group-standard: padding, margin; // 标准方向属性
  11. $group-radius: border-radius; // 圆角属性
  12. $group-border: border, border-top, border-right, border-bottom, border-left; // 边框属性
  13. $group-outline: outline, outline-top, outline-right, outline-bottom, outline-left; // 轮廓属性
  14. $group-position: inset, inset-block, inset-inline, top, right, bottom, left; // 定位属性
  15. $group-size: block-size, inline-size; // 块/行尺寸属性
  16. // 定义每个方向的值
  17. $top: null;
  18. $right: null;
  19. $bottom: null;
  20. $left: null;
  21. // 确定处理模式
  22. $is-standard: index($group-standard, $property);
  23. $is-radius: index($group-radius, $property);
  24. $is-border: index($group-border, $property);
  25. $is-outline: index($group-outline, $property);
  26. $is-position: index($group-position, $property);
  27. $is-size: index($group-size, $property);
  28. // =====================================================================
  29. // 解析输入值 - 根据属性类型处理
  30. // =====================================================================
  31. @if type-of($values) == 'list' {
  32. $length: length($values);
  33. // 单个值 - 所有方向相同值
  34. @if $length == 1 {
  35. $top: nth($values, 1);
  36. $right: nth($values, 1);
  37. $bottom: nth($values, 1);
  38. $left: nth($values, 1);
  39. }
  40. // 两个值
  41. @else if $length == 2 {
  42. // 特殊处理:border-radius
  43. @if $is-radius {
  44. $top: nth($values, 1);
  45. $right: nth($values, 2);
  46. $bottom: nth($values, 1);
  47. $left: nth($values, 2);
  48. }
  49. // 标准处理:上下 | 左右
  50. @else if $is-standard or $is-border or $is-outline or $is-position {
  51. $top: nth($values, 1);
  52. $bottom: nth($values, 1);
  53. $right: nth($values, 2);
  54. $left: nth($values, 2);
  55. }
  56. // 块/行尺寸处理
  57. @else if $is-size {
  58. $top: nth($values, 1);
  59. $right: nth($values, 2);
  60. }
  61. }
  62. // 三个值
  63. @else if $length == 3 {
  64. // border-radius特殊处理
  65. @if $is-radius {
  66. $top: nth($values, 1);
  67. $right: nth($values, 2);
  68. $bottom: nth($values, 3);
  69. $left: nth($values, 2);
  70. }
  71. // 标准处理
  72. @else if $is-standard or $is-border or $is-outline or $is-position {
  73. $top: nth($values, 1);
  74. $right: nth($values, 2);
  75. $left: nth($values, 2);
  76. $bottom: nth($values, 3);
  77. }
  78. }
  79. // 四个值
  80. @else if $length == 4 {
  81. $top: nth($values, 1);
  82. $right: nth($values, 2);
  83. $bottom: nth($values, 3);
  84. $left: nth($values, 4);
  85. }
  86. }
  87. @else {
  88. // 单个值传递
  89. $top: $values;
  90. $right: $values;
  91. $bottom: $values;
  92. $left: $values;
  93. }
  94. // =====================================================================
  95. // 生成CSS属性(排除指定方向)
  96. // =====================================================================
  97. // 1. 圆角处理
  98. @if $is-radius {
  99. @if not index($exclude, top-left) and $top != null {
  100. border-top-left-radius: $top;
  101. }
  102. @if not index($exclude, top-right) and $right != null {
  103. border-top-right-radius: $right;
  104. }
  105. @if not index($exclude, bottom-right) and $bottom != null {
  106. border-bottom-right-radius: $bottom;
  107. }
  108. @if not index($exclude, bottom-left) and $left != null {
  109. border-bottom-left-radius: $left;
  110. }
  111. }
  112. // 2. 标准方向处理 (padding, margin)
  113. @else if $is-standard {
  114. @if not index($exclude, top) and $top != null {
  115. #{$property}-top: $top;
  116. }
  117. @if not index($exclude, right) and $right != null {
  118. #{$property}-right: $right;
  119. }
  120. @if not index($exclude, bottom) and $bottom != null {
  121. #{$property}-bottom: $bottom;
  122. }
  123. @if not index($exclude, left) and $left != null {
  124. #{$property}-left: $left;
  125. }
  126. }
  127. // 3. 边框处理
  128. @else if $is-border {
  129. // 完整边框设置
  130. @if $property == 'border' {
  131. @if not index($exclude, top) and $top != null {
  132. border-top-width: nth($values, 1);
  133. border-top-style: nth($values, 2);
  134. border-top-color: nth($values, 3);
  135. }
  136. @if not index($exclude, right) and $right != null {
  137. border-right-width: nth($values, 1);
  138. border-right-style: nth($values, 2);
  139. border-right-color: nth($values, 3);
  140. }
  141. @if not index($exclude, bottom) and $bottom != null {
  142. // border-bottom: $bottom;
  143. border-bottom-width: nth($values, 1);
  144. border-bottom-style: nth($values, 2);
  145. border-bottom-color: nth($values, 3);
  146. }
  147. @if not index($exclude, left) and $left != null {
  148. // border-left: $left;
  149. border-left-width: nth($values, 1);
  150. border-left-style: nth($values, 2);
  151. border-left-color: nth($values, 3);
  152. }
  153. }
  154. // 单边边框
  155. @else {
  156. $direction: str-slice($property, 8); // 提取方向
  157. @if not index($exclude, $direction) {
  158. // #{$property}: $top;
  159. #{$property}-width: nth($values, 1);
  160. #{$property}-style: nth($values, 2);
  161. #{$property}-color: nth($values, 3);
  162. }
  163. }
  164. }
  165. // 4. 轮廓处理
  166. @else if $is-outline {
  167. // 完整轮廓设置
  168. @if $property == 'outline' {
  169. @if not index($exclude, top) and $top != null {
  170. outline-top: $top;
  171. }
  172. @if not index($exclude, right) and $right != null {
  173. outline-right: $right;
  174. }
  175. @if not index($exclude, bottom) and $bottom != null {
  176. outline-bottom: $bottom;
  177. }
  178. @if not index($exclude, left) and $left != null {
  179. outline-left: $left;
  180. }
  181. }
  182. // 单边轮廓
  183. @else {
  184. $direction: str-slice($property, 8); // 提取方向
  185. @if not index($exclude, $direction) {
  186. #{$property}: $top;
  187. }
  188. }
  189. }
  190. // 5. 定位处理
  191. @else if $is-position {
  192. // inset简写处理
  193. @if $property == 'inset' {
  194. @if not index($exclude, top) and $top != null {
  195. top: $top;
  196. }
  197. @if not index($exclude, right) and $right != null {
  198. right: $right;
  199. }
  200. @if not index($exclude, bottom) and $bottom != null {
  201. bottom: $bottom;
  202. }
  203. @if not index($exclude, left) and $left != null {
  204. left: $left;
  205. }
  206. }
  207. // inset-block 和 inset-inline
  208. @else if $property == 'inset-block' {
  209. @if not index($exclude, top) and $top != null {
  210. top: $top;
  211. }
  212. @if not index($exclude, bottom) and $bottom != null {
  213. bottom: $bottom;
  214. }
  215. }
  216. @else if $property == 'inset-inline' {
  217. @if not index($exclude, left) and $left != null {
  218. left: $left;
  219. }
  220. @if not index($exclude, right) and $right != null {
  221. right: $right;
  222. }
  223. }
  224. // 单一定位
  225. @else {
  226. @if not index($exclude, $property) {
  227. #{$property}: $top;
  228. }
  229. }
  230. }
  231. // 6. 尺寸处理
  232. @else if $is-size {
  233. @if $property == 'block-size' {
  234. @if not index($exclude, top) and $top != null {
  235. height: $top;
  236. }
  237. }
  238. @else if $property == 'inline-size' {
  239. @if not index($exclude, left) and $left != null {
  240. width: $left;
  241. }
  242. }
  243. }
  244. }
  245. // =======================================================================
  246. // 快捷混合宏:标准属性
  247. // =======================================================================
  248. @mixin padding($values, $exclude: ()) {
  249. @include directional-property(padding, $values, $exclude);
  250. }
  251. @mixin margin($values, $exclude: ()) {
  252. @include directional-property(margin, $values, $exclude);
  253. }
  254. @mixin border-radius($values, $exclude: ()) {
  255. @include directional-property(border-radius, $values, $exclude);
  256. }
  257. // =======================================================================
  258. // 快捷混合宏:边框属性
  259. // =======================================================================
  260. @mixin border($value, $exclude: ()) {
  261. @include directional-property(border, $value, $exclude);
  262. }
  263. @mixin border-top($value, $exclude: ()) {
  264. @include directional-property(border-top, $value, $exclude);
  265. }
  266. @mixin border-right($value, $exclude: ()) {
  267. @include directional-property(border-right, $value, $exclude);
  268. }
  269. @mixin border-bottom($value, $exclude: ()) {
  270. @include directional-property(border-bottom, $value, $exclude);
  271. }
  272. @mixin border-left($value, $exclude: ()) {
  273. @include directional-property(border-left, $value, $exclude);
  274. }
  275. // =======================================================================
  276. // 快捷混合宏:轮廓属性
  277. // =======================================================================
  278. @mixin outline($value, $exclude: ()) {
  279. @include directional-property(outline, $value, $exclude);
  280. }
  281. @mixin outline-top($value, $exclude: ()) {
  282. @include directional-property(outline-top, $value, $exclude);
  283. }
  284. @mixin outline-right($value, $exclude: ()) {
  285. @include directional-property(outline-right, $value, $exclude);
  286. }
  287. @mixin outline-bottom($value, $exclude: ()) {
  288. @include directional-property(outline-bottom, $value, $exclude);
  289. }
  290. @mixin outline-left($value, $exclude: ()) {
  291. @include directional-property(outline-left, $value, $exclude);
  292. }
  293. // =======================================================================
  294. // 快捷混合宏:定位属性
  295. // =======================================================================
  296. @mixin inset($values, $exclude: ()) {
  297. @include directional-property(inset, $values, $exclude);
  298. }
  299. @mixin inset-block($values, $exclude: ()) {
  300. @include directional-property(inset-block, $values, $exclude);
  301. }
  302. @mixin inset-inline($values, $exclude: ()) {
  303. @include directional-property(inset-inline, $values, $exclude);
  304. }
  305. @mixin top($value, $exclude: ()) {
  306. @include directional-property(top, $value, $exclude);
  307. }
  308. @mixin right($value, $exclude: ()) {
  309. @include directional-property(right, $value, $exclude);
  310. }
  311. @mixin bottom($value, $exclude: ()) {
  312. @include directional-property(bottom, $value, $exclude);
  313. }
  314. @mixin left($value, $exclude: ()) {
  315. @include directional-property(left, $value, $exclude);
  316. }
  317. // =======================================================================
  318. // 快捷混合宏:尺寸属性
  319. // =======================================================================
  320. @mixin block-size($value, $exclude: ()) {
  321. @include directional-property(block-size, $value, $exclude);
  322. }
  323. @mixin inline-size($value, $exclude: ()) {
  324. @include directional-property(inline-size, $value, $exclude);
  325. }
  326. // =======================================================================
  327. // 组合混合宏
  328. // =======================================================================
  329. // 带圆角的边框
  330. @mixin bordered($border-value, $radius-value) {
  331. @include border($border-value);
  332. @include border-radius($radius-value);
  333. }
  334. // 绝对定位容器
  335. @mixin absolute-container($inset: 0) {
  336. position: absolute;
  337. @include inset($inset);
  338. }
  339. // 固定定位容器
  340. @mixin fixed-container($inset: 0) {
  341. position: fixed;
  342. @include inset($inset);
  343. }
  344. // =======================================================================
  345. // 圆角辅助混合宏
  346. // =======================================================================
  347. @mixin border-top-radius($value) {
  348. @include border-radius($value 0 0, (bottom-right, bottom-left));
  349. }
  350. @mixin border-right-radius($value) {
  351. @include border-radius(0 $value 0 0, (top-left, bottom-left));
  352. }
  353. @mixin border-bottom-radius($value) {
  354. @include border-radius(0 0 $value, (top-right, top-left));
  355. }
  356. @mixin border-left-radius($value) {
  357. @include border-radius(0 0 0 $value, (top-right, bottom-right));
  358. }