Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. # Third Party Styling
  2.  
  3. ## Introduction/Brief
  4.  
  5. Intercom has this widget in the bottom right hand corner that does magic things.
  6. We need it to shift up and down according to our own app's behavior, to avoid blocking stuff.
  7.  
  8. ## Constraints
  9.  
  10. 1. Intercom built in a really easy way of adjusting the vertical padding of their component, but because we implemented intercom through Segment, we have no access to configure intercom. 😞
  11.  
  12. 2. Intercom's components are `iframe` elements (a few of them actually). Luckily we can apply styles to the iframes, and this allows us to move the widgets up and down. But we may have to apply slightly different styles to each iframe, instead of applying universal styles to a single iframe.
  13.  
  14. 3. The widget has to dynamically reposition itself on the same screen in response to user behavior, and support multiple "levels" of padding
  15.  
  16. 4. The widget still has to be on all our other screens at the original position.
  17.  
  18. 5. The widget is not necessarily present at first load or even at all (pesky adblockers).
  19.  
  20. ## Methods explored
  21.  
  22. We ultimately chose the Dynamic Stylesheet Injection as it
  23.  
  24. ### Dynamic Style Injection via the `style` _attribute_
  25.  
  26. 1. Find the specific iframe elements when repositioning is required
  27. 2. If found, apply specific styles to those elements, via dom manipulation
  28.  
  29. ```javascript
  30. if (low) {
  31. return document.getElementsByName('intercom-launcher-frame')[0]
  32. .style
  33. .marginBottom = "75px"
  34. }
  35.  
  36. return document.getElementsByName('intercom-launcher-frame')[0]
  37. .style
  38. .marginBottom = "145px"
  39. ```
  40.  
  41. GOOD:
  42.  
  43. - pretty low effort change, and really simple and direct
  44.  
  45. BAD:
  46.  
  47. - Elements were not necessarily there when we needed them (especially first load)
  48. - When they first loaded, they could not be repositioned until a repositioning event occured.
  49.  
  50. ### Dynamic Class Injection with a static stylesheet
  51.  
  52. 1. Load a static stylesheet with our custom padding styles
  53.  
  54. ```css
  55. .intercom-padding-low {
  56. margin-bottom: 75px;
  57. }
  58.  
  59. .intercom-padding-high {
  60. margin-bottom: 145px;
  61. }
  62. ```
  63.  
  64. 2. Find the specific iframe elements when repositioning is required and inject the specific class names for the specific level of padding required
  65.  
  66. ```javascript
  67. if (low) {
  68. return document.getElementsByName('intercom-launcher-frame')[0]
  69. .classList
  70. .add('intercom-padding-low')
  71. }
  72.  
  73. return document.getElementsByName('intercom-launcher-frame')[0]
  74. .classList
  75. .add('intercom-padding-high')
  76. ```
  77.  
  78. GOOD:
  79.  
  80. - static styles?
  81.  
  82. BAD:
  83.  
  84. - Same as above
  85. - Plus now we have to define custom css for each level of padding _wtf we are not doing this._
  86.  
  87. ### Dynamic Stylesheet Injection via a `style` _element_
  88.  
  89. 1. _Render_ a `<style>` tag conditionally with dynamically set css. Css in the dynamic stylesheet targets the original classes
  90.  
  91. ```javascript
  92. render() {
  93. return (
  94. <style type="text/css">
  95. {`
  96. .intercom-launcher-frame {
  97. margin-bottom: ${margin}px
  98. }
  99. `}
  100. </style>
  101. )
  102. }
  103. ```
  104.  
  105. GOOD:
  106.  
  107. - Wow I get to actually write this declaratively as react code instead of some imperative dom js hack
  108. - Because this stylesheet targets the original classes, it's in effect on first load, and whenever 😄
  109.  
  110. BAD:
  111.  
  112. - This is not a standard way of writing styles, so tooling support is poor (no syntax highlighting for example)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement