This repository has been archived by the owner on Jan 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Make TS easier to wrap (#4407)
### Goal Make it easier for frameworks to wrap our TypeScript code. Refs #4225 ### How **Isolate framework-specific code from framework-agnostic code.** Specifically, this PR: * Maintains full backward compatibility with existing code * Moves all component definitions from `index.ts` to `component.ts` - `index.ts` is now purely re-exporting other files * Moves component-dependent types from `types.ts` to `component.ts` - Framework-related types are now quarantined in a single file: `component.ts` - All other files are now purely framework-agnostic, and can be safely wrapped by frameworks that don't use our components * Makes `import` paths more specific by switching from `@material/foo/index` to e.g. `@material/foo/foundation` or `@material/foo/types` - The only exception is `component.ts` files: Since they're basically our default "framework", they can safely import _other_ "framework" types via `@material/foo/index` * Updates class & interface export syntax to isolate the `default` export line for future removal if necessary: ```ts export class MDCFooFoundation { // ... } export default MDCFooFoundation; ``` * Combines `typings/custom.d.ts` and `typings/dom.ie.d.ts` into `packages/mdc-dom/externs.d.ts` so the types will be publicly visible * Adds `MDCFooFactory` types for components that need them (e.g., `MDCRipple`, `MDCList`) ### Packages * [x] `animation` * [x] `checkbox` * [x] `chips` * [x] `dialog` * [x] `dom` * [x] `drawer` * [x] `floating-label` * [x] `form-field` * [x] `grid-list` * [x] `icon-button` * [x] `icon-toggle` _(deprecated)_ * [x] `line-ripple` * [x] `linear-progress` * [x] `list` * [x] `menu` * [x] `menu-surface` * [x] `notched-outline` * [x] `radio` * [x] `ripple` * [x] `select` * [x] `selection-control` * [x] `slider` * [x] `snackbar` * [x] `switch` * [x] `tab` * [x] `tab-bar` * [x] `tab-indicator` * [x] `tab-scroller` * [x] `tab-tabs` _(deprecated)_ * [x] `textfield` * [x] `toolbar` _(deprecated)_ * [x] `top-app-bar`
- Loading branch information
Showing
245 changed files
with
5,600 additions
and
5,073 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,22 @@ | ||
node_modules | ||
# macOS filesystem | ||
.DS_Store | ||
/build | ||
/coverage | ||
packages/*/dist | ||
out | ||
|
||
# Editors and IDEs | ||
.idea/ | ||
.vscode/ | ||
|
||
# Generated files | ||
*.log | ||
.idea | ||
*.sw* | ||
.closure-tmp | ||
.site-generator-tmp | ||
.typescript-tmp | ||
.vscode | ||
node_modules/ | ||
|
||
# Build output | ||
/build/ | ||
/coverage/ | ||
packages/*/dist | ||
|
||
# Material.io site generator test (`npm run test:site`) | ||
.site-generator-tmp/ | ||
|
||
# Used by internal sync & rewrite scripts | ||
.rewrite-tmp/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* @license | ||
* Copyright 2016 Google Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
import { | ||
CssVendorPropertyMap, JsVendorPropertyMap, | ||
PrefixedCssPropertyName, PrefixedJsEventType, | ||
StandardCssPropertyName, StandardJsEventType, | ||
} from './types'; | ||
|
||
const cssPropertyNameMap: CssVendorPropertyMap = { | ||
animation: { | ||
prefixed: '-webkit-animation', | ||
standard: 'animation', | ||
}, | ||
transform: { | ||
prefixed: '-webkit-transform', | ||
standard: 'transform', | ||
}, | ||
transition: { | ||
prefixed: '-webkit-transition', | ||
standard: 'transition', | ||
}, | ||
}; | ||
|
||
const jsEventTypeMap: JsVendorPropertyMap = { | ||
animationend: { | ||
cssProperty: 'animation', | ||
prefixed: 'webkitAnimationEnd', | ||
standard: 'animationend', | ||
}, | ||
animationiteration: { | ||
cssProperty: 'animation', | ||
prefixed: 'webkitAnimationIteration', | ||
standard: 'animationiteration', | ||
}, | ||
animationstart: { | ||
cssProperty: 'animation', | ||
prefixed: 'webkitAnimationStart', | ||
standard: 'animationstart', | ||
}, | ||
transitionend: { | ||
cssProperty: 'transition', | ||
prefixed: 'webkitTransitionEnd', | ||
standard: 'transitionend', | ||
}, | ||
}; | ||
|
||
function isWindow(windowObj: Window): boolean { | ||
return Boolean(windowObj.document) && typeof windowObj.document.createElement === 'function'; | ||
} | ||
|
||
export function getCorrectPropertyName(windowObj: Window, cssProperty: StandardCssPropertyName): | ||
StandardCssPropertyName | PrefixedCssPropertyName { | ||
if (isWindow(windowObj) && cssProperty in cssPropertyNameMap) { | ||
const el = windowObj.document.createElement('div'); | ||
const {standard, prefixed} = cssPropertyNameMap[cssProperty]; | ||
const isStandard = standard in el.style; | ||
return isStandard ? standard : prefixed; | ||
} | ||
return cssProperty; | ||
} | ||
|
||
export function getCorrectEventName(windowObj: Window, eventType: StandardJsEventType): | ||
StandardJsEventType | PrefixedJsEventType { | ||
if (isWindow(windowObj) && eventType in jsEventTypeMap) { | ||
const el = windowObj.document.createElement('div'); | ||
const {standard, prefixed, cssProperty} = jsEventTypeMap[eventType]; | ||
const isStandard = cssProperty in el.style; | ||
return isStandard ? standard : prefixed; | ||
} | ||
return eventType; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.