useBackgrounds.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import useVars from './useVars.js'
  2. import { getClasses } from '../utils/classes.js'
  3. import useColor from './useColor.js'
  4. function useHook() { //
  5. const { colors, color } = useColor()
  6. const { pxs, percents } = useVars()
  7. const BackgroundAttachment = { title: 'Background Attachment', classes: getClasses(`fixed, local, scroll`, { name: 'bg-', value: 'background-attachment', scss: 0 }) }
  8. const BackgroundClip = {
  9. title: 'Background Clip',
  10. classes: getClasses([
  11. { name: 'border', value: 'border-box' },
  12. { name: 'padding', value: 'padding-box' },
  13. { name: 'content', value: 'content-box' },
  14. { name: 'text', value: 'text' },
  15. ], { name: 'bg-clip-', value: 'background-clip', scss: 0 })
  16. }
  17. const BackgroundColor = {
  18. title: 'Background Color',
  19. classes: color.map(c => {
  20. return {
  21. ...c,
  22. name: `bg-${c.name}`,
  23. value: [`background: ${c.value};`],
  24. bgcolor: c.color,
  25. desc: c.rgb,
  26. }
  27. })
  28. }
  29. const BackgroundOrigin = {
  30. title: 'Background Origin',
  31. classes: getClasses([{ name: 'border', value: 'border-box' },
  32. { name: 'padding', value: 'padding-box' },
  33. { name: 'content', value: 'content-box' },
  34. ], { name: 'bg-origin-', value: 'background-origin', scss: 0 })
  35. }
  36. const BackgroundPosition = {
  37. title: 'Background Position',
  38. classes: getClasses([
  39. { name: 'bottom', value: 'bottom' },
  40. { name: 'center', value: 'center' },
  41. { name: 'left', value: 'left' },
  42. { name: 'left-bottom', value: 'left bottom' },
  43. { name: 'left-top', value: 'left top' },
  44. { name: 'right', value: 'right' },
  45. { name: 'right-bottom', value: 'right bottom' },
  46. { name: 'right-top', value: 'right top' },
  47. { name: 'top', value: 'top' },
  48. ], { name: 'bg-', value: 'background-position', scss: 0 })
  49. }
  50. const BackgroundRepeat = {
  51. title: 'Background Repeat',
  52. classes: getClasses([
  53. { name: 'repeat', value: 'repeat' },
  54. { name: 'no-repeat', value: 'no-repeat' },
  55. { name: 'repeat-x', value: 'repeat-x' },
  56. { name: 'repeat-y', value: 'repeat-y' },
  57. { name: 'repeat-round', value: 'round' },
  58. { name: 'repeat-space', value: 'space' },
  59. ], { name: 'bg-', value: 'background-repeat', scss: 0 })
  60. }
  61. const BackgroundSize = { title: 'Background Size', classes: getClasses(`auto, cover, contain`, { name: 'bg-', value: 'background-size', scss: 0 }) }
  62. const BackgroundImage = {
  63. title: 'Background Image',
  64. classes: getClasses([
  65. { name: 'none', value: 'none' },
  66. { name: 'gradient-to-t', value: 'linear-gradient(to top, var(--gradient-stops))' },
  67. { name: 'gradient-to-tr', value: 'linear-gradient(to top right, var(--gradient-stops))' },
  68. { name: 'gradient-to-r', value: 'linear-gradient(to right, var(--gradient-stops))' },
  69. { name: 'gradient-to-br', value: 'linear-gradient(to bottom right, var(--gradient-stops))' },
  70. { name: 'gradient-to-b', value: 'linear-gradient(to bottom, var(--gradient-stops))' },
  71. { name: 'gradient-to-bl ', value: 'linear-gradient(to bottom left, var(--gradient-stops))' },
  72. { name: 'gradient-to-l', value: 'linear-gradient(to left, var(--gradient-stops))' },
  73. { name: 'gradient-to-tl', value: 'linear-gradient(to top left, var(--gradient-stops))' },
  74. ], { name: 'bg-', value: 'background-image', scss: 0 })
  75. }
  76. function getFromColor() {
  77. return color.map(c => {
  78. const v = c.var ? `var(${c.value})` : c.value
  79. return {
  80. ...c,
  81. name: `from-${c.name}`,
  82. value: [
  83. `--gradient-from-position: 0%;`,
  84. `--gradient-from: ${v} var(--gradient-from-position);`,
  85. `--gradient-to-position: 100%;`,
  86. `--gradient-to: rgba(0,0,0,0) var(--gradient-to-position);`,
  87. `--gradient-stops: var(--gradient-from), var(--gradient-to);`,
  88. ],
  89. desc: c.rgb,
  90. }
  91. })
  92. }
  93. function getToColor() {
  94. return color.map(c => {
  95. const v = c.var ? `var(${c.value})` : c.value
  96. return {
  97. ...c,
  98. name: `to-${c.name}`,
  99. value: [
  100. `--gradient-to: ${v} var(--gradient-to-position);`,
  101. ],
  102. desc: c.rgb,
  103. }
  104. })
  105. }
  106. function getPercentFromToColor() {
  107. let froms = new Array(9).fill(0).map((d, i) => {
  108. return {
  109. name: `.from-0-${i + 1}`,
  110. value: [`--gradient-from-position: calc(${i + 1} * 10 * 1%);`]
  111. }
  112. })
  113. let tos = new Array(9).fill(0).map((d, i) => {
  114. return {
  115. name: `.to-0-${i + 1}`,
  116. value: [`--gradient-to-position: calc(${i + 1} * 10 * 1%);`]
  117. }
  118. })
  119. return [...froms, ...tos]
  120. }
  121. const GradientColorStops = {
  122. title: 'Gradient Color Stops',
  123. classes: [
  124. ...getFromColor(),
  125. ...getToColor(),
  126. ...getPercentFromToColor(),
  127. ]
  128. }
  129. return {
  130. BackgroundAttachment,
  131. BackgroundClip,
  132. BackgroundColor,
  133. BackgroundOrigin,
  134. BackgroundPosition,
  135. BackgroundRepeat,
  136. BackgroundSize,
  137. BackgroundImage,
  138. GradientColorStops
  139. }
  140. }
  141. export default useHook