Guest User

Untitled

a guest
Jun 18th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. # React guidelines to support content theming in Open edX (Braden's proposal)
  2.  
  3. 1. Build the UI out of small, modular React components as much as possible.
  4. 2. Build two types of components: "customizable" ones that only compose others
  5. using JSX and contain little-or-no HTML nor logic, as well as "internal"
  6. components that contain logic and detailed HTML and CSS, etc.
  7. 3. In customizable components, include placeholders like `{this.extraContent}`
  8. in the render() method so that subclasses don't have to override render().
  9.  
  10. Bad example:
  11.  
  12. ```typescript
  13. class Header extends React.PureComponent<Props, State> {
  14. public render() {
  15. return (
  16. <header>
  17. <div className="logoArea">
  18. <img src={this.props.brandedLogo} alt={this.props.siteName} />
  19. </div>
  20. <div className="mainNav">
  21. <ul>
  22. <li>
  23. <a href="/">Home</a>
  24. {this.props.isLoggedIn ? null : <a href="/login">Login</a>}
  25. </li>
  26. </ul>
  27. </div>
  28. <div class="currentUserAvatar">
  29. <!-- User avatar, account menu etc. -->
  30. </div>
  31. </header>
  32. );
  33. }
  34. }
  35. ```
  36.  
  37. Good example:
  38.  
  39. ```typescript
  40. // Customizable Header
  41. class _Header extends React.PureComponent<Props, State> {
  42. public render() {
  43. return (
  44. <header>
  45. <SiteLogo/>
  46. <MainNav/>
  47. <UserAvatar/>
  48. </header>
  49. );
  50. }
  51. }
  52. // Customizable Main Navigation Area
  53. class _MainNav extends React.PureComponent<Props> {
  54. public render() {
  55. return (
  56. <MainNavWrapper>
  57. <a href="/">Home</a>
  58. <LoginLink/>
  59. {this.extraNavLinks}
  60. </MainNavWrapper>
  61. );
  62. }
  63. get extraNavLinks(): JSX.Element[] { return []; }
  64. }
  65. // Internal MainNavWrapper - not meant to be modified in most cases)
  66. class _MainNavWrapper extends React.PureComponent<Props> {
  67. public render() {
  68. return <div className="mainNav">
  69. <ul>
  70. {React.Children.map(this.props.children, (child) => (child ? <li>{child}</li> : null))}
  71. </ul>
  72. </div>;
  73. }
  74. }
  75.  
  76. // Default Theme:
  77. export const Header = _Header;
  78. export const MainNav = _MainNav;
  79. export const MainNavWrapper = _MainNavWrapper;
  80. ```
  81.  
  82. And here's an example of a custom theme:
  83.  
  84. ```typescript
  85. class MyThemedHeader extends _Header {
  86. public render() {
  87. return (
  88. <header>
  89. {/* Replace <SiteLogo/> with a fancy widget */}
  90. <MyCustomAnimatedLogoWidget/>
  91. <MainNav/>
  92. <UserAvatar/>
  93. </header>
  94. );
  95. }
  96. }
  97. // Customizable Main Navigation Area
  98. class MyThemedNav extends _MainNav {
  99. get extraNavLinks() {
  100. return [
  101. <a href="/about">About Us</a>,
  102. ];
  103. }
  104. }
  105.  
  106. // My theme:
  107. export const Header = MyThemedHeader;
  108. export const MainNav = MyThemedNav
  109. export const MainNavWrapper = _MainNavWrapper;
  110. ```
  111.  
  112. I've left out all the Redux glue code, etc., but you can get the idea.
Add Comment
Please, Sign In to add comment